Refactoring, add tests.