Working nested loop