fixes  an error in a for loop variable that had a wide variety of undesired side effects

Fixes: #2521