moved globals, to deal with race condition on multiple iterations