Cleaned up code. Added docstrings.