Refactored the code to reduce the size of some functions. Also stripped out an aborted attempt at chat functionality