Code refactored with additional unit tests