move more control to js code
refactor main.cpp