refactoring retry logic to middleware. getting all tests to pass