refactoring and added tests