Code idempotent functions

The functions should produce the same result even after calling them multiple times. This helps in retrying an invocation if the previous invocation fails partway through our code.