refactoring with test cases
fixed merge conflict