moved code outside of loop