Refactored code; added new test