change while to foreach