Refactored code and test-cases.