Refactoring and added tests