Add tests and fix edge cases.