changed loop to correct variable iterations