Fix many many bugs

All tests now succeed.