Extract js code to its own file (and a few other tweaks)