Extracted for loop reduces code duplication.