Fix some small bugs.
Add tests.