Added test cases and refactored code