Refactored code; included unit tests