minor refactoring of large for loop