Refactored retry code. Uses exponential backoff