Refactored main application flow to be more readable