Refactored and added testing