Refactored some code and added in test scenarios.