rewrite with foreach