nested loop implementation