Refactored and added tests