Rework error checking a little