Fix bugs. Add tests.