Added comments and docstrings to the majority of functions