Refactor JS a bit