Move some misc setup functions out of main to make things neater.