reorganize js code