Factor out some code