fix main.js crash. sub functions must be at top