move some more code out of main()