fix a few bugs found during code review