Fixed bugs, refactored, added documentation and unit tests.