Adding unit tests and starting code refactor