webchat: refactored code