refactor chat js