Added more logging and error checking.