Refactoring and adding unit tests.
Refactoring and adding unit tests