Factor out into a separate function