Corrected nested looping