Fix bugs and tests.