some of these variables can be moved into the loop below.