Fixed local variable escaping from foreach loop