Fix duplicate variable in nested loop