split out some of this from main()