Code cleanup and better docstrings