Add tests, fix bugs, refactor.