Refactoring and adding tests.