Added tests for retries and fixes bugs found by tests along the way