Refactored to clean up the code. Added functionality to send and receive messages