a lot of small bug fixes to make the tests pass