changed for loop to foreach loop