Replace foreach with pipelines, for improved memory efficiency.