fix a few nasty bugs