Attempt to fix resource leak when API fails

Also added some sleeps to be polite to the api server