def _beautify_message(msg):
    """
    Function for cleaning the received message. Removes everything not allowed by the specification, adds some required
    fields and renames some to follow the specification.

    :param msg: Received message as dictionary

    :return: Cleaned message.

    """
    msg["type"] = "Message"
    msg.pop("_id")
    msg["id"] = msg.pop("mid")
    if "attachments" in msg:
        if len(msg["attachments"]) > 0:
            for item in msg["attachments"]:
                item["id"] = item.pop("aid")
        elif len(msg["attachments"]) == 0:
            del msg["attachments"]
    return msg