Replace several for-each loops with lambdas