Refactor to allow retries