Enhanced error handling and logging.