Fix unneeded escaping
Improve scoping in main()
Isolate setup into its own function
Make advance to next states a function