Added nested for loop and other changes.