Factor out some duplication