Change for loop to foreach