extensive improvements to error checking