Fixes to a few horrible bugs