Refactoring + tests.