Added docstrings. Some code cleanup