improve chat and refactor code in several places