Refactored /chat endpoint