Factor out reused code.