moving retry code to a function and build up the if else logic