Add tests and refactor.