Refactored for loop into linq
Refactored out some logic into extension methods