Refactored and added a test case