Added more tests and docstrings, purified code a bit