Added check to foreach loop