nested loops and data linkage