Fixes undefined loop variables (fixes #657)