Some refactorization for the main program.