Factor out some common code