small bug fix for loop function

Hi, Andrew Gerrand, thanks for your speech and the demo code.
But I think you have mistype some variable in your loop function, and here is the fix.
Thanks again.