Refactored and added docstrings