replacing some for each loops with LINQ