Fixed bug where nested loop did not act as expected.