Refactored main workflow.