Much more thorough error handling and logging.