Clean up docstrings, refactor some common logic

Added a bunch of docstrings to the function, and I refactored some of the common logic out of retry() and retrying()