Minor bug fixes (getting rid of some potential infinite loops)