replaced foreach loop