Refactor chat.js.