switched foreach loops to for