Refactor the js a bit