Factor out common code.