Update whil loops with foreach