refactored code and test