Add tests and fix bugs.