more refactoring and test cases