Moved output statements inside outer loop