cleaned up some variables and iteration flow