fixed index error caused by miscalculating max iterations