Updated error checking code