* refactored some redundant code and added docstrings