refactored chat alogrithm now with initialization