refactoring and adding test