restructure codes in main.js