Fixed errors in loops so they exit instead of giving a million error pop-up boxes.