refactoring and adding tests