updated with foreach loop