fix an issue in retries, and add tests