Refactor JS a little