small bug fix for loop function