Added unit test and refactor first draft