Refactoring and test all pass