move variable declaration outside of loop to make closure work