Fix error in foreach loop