Changed all for-in (foreach) loops to regular for loops