Moved some code out of main()