Fix bugs and make the tests pass, sort of.