Factor out some functionality