reorganized the code in main.c