fix a few subtle minor bugs