Added more exception code and error checking.