Added docstrings and refactored