Fix for loop variable duplicate bug