Added workaround for poorly defined foreach (see comments).