webchat: refactor code