Refactored a bit. Added some documentation.
Added some unit tests.