Bug fixes: use foreach to replace array_walk