Refactoring: extract 'for' cycle with logic in function.