Move foreach loop out into extracted method