(hopefully) fixed error checking so it won't infinite loop