change while loops with foreach