Cleaned up code, added docstrings.