Added docstrings, comments, and 'pythonized' the code a little