Done some refactoring, added docstrings, added some tests, fixed some minor bugs