Factor out routing code