Refactored some code
Added unit tests