refactored chat.js