replacing for with foreach