improved retry logic