Add tests and fix uncovered bugs.