refactor retry helper function