Adding a big test case and refactoring logic.