change while to foreach loops