Improved error checking and reporting.