Code cleanup, added docstrings