Factor out the stuff main does.