refactored and added tests