Extract Method refactoring to reduce nesting of loops.