Adding docstrings, tests and fixing bugs.