added logic to chat.js