Misc bug fixes (hopefully didn't break more things than I fixed)