replaced iterators with indexed loops