Fixes undefined loop variables (fixes #657)

A fix for #657