Refactor main function.