#817 Rewrite whole Javascript to be much simpeler