Improve error checking and return more accurate errors.