moved the printing of output outside of loop