Lots of miscellaneous bug fixes to pass tests