refatoring: extracted chat function