Refactor main program.