Fix some bugs and fix tests.