More optimizations

Remade some foreach statements where there's no reason to have array functions calls inside of it.