writing unit test and refactoring