Changed for loops to use forEach