Simplify main() logic.

Add Application class.
Put application initialization code to Application class.
Place some code into separate functions.