refactoring chat codes