write test cases and refactoring