Updated error checking