simplify _foreach code