Refactored and tested