Refactor chat code to use callbacks.