Added protection again foreach loops failing due to variables not being initialized