Rewrite JS to be more modular