Reorganized the functions and variables of main