Moving remaining code from outer into inner loop.