Refactored and added test