Added docstrings, refactored a bit