chat example refactored