better error checking and reporting