Refactoring implementation and tests.