Factor out repeated code