Moved the chat loop in a separate function