Refactored to use more appropriate Enumerable methods.