replaced foreach with for loop