Add a little more defensive error checking