Replaced normal for loops with foreach loops which is less error prone w...

...hen just iterating over collection/array