Pulling out inner processing loop to its own function.