Refactored chat functionality