Added comments for the message code and fixed a few bugs