Added some unit tests.
Refactored some code