refactoring and added test