added docstrings, minor refactoring