clean up, rewrite main JS