adds retry loop and adds/improves logging.