improve chat state logic