Fix scoping issues within nested loops.