Added more robust error detection and reporting.