Refactored some fo the code. Still a mess on the chat part.