Common code from main()s factored out