More robust error checking