Added better error checking and reporting mechanisms.