Added two more test cases.
Refactored algorithm + some optimizations.
Minor refactoring.