Adhere more closely to PEP7

Also, move as much code as possible out of main()