change for loops to foreach