Split out the large main function a bit.