Refactor. Add tests.