Fixed error in foreach loop