Added error handling and logging.
More error handling and more logging.