def metadata_to_dict(metadata):
    """
    Takes metadata as specified in seperate_metadata_and_content() documentation and\
    converts it to a dictionary.
    Note that white space on either side of the metadata tags and values will be stripped; \
    also, if there are any duplicate metadata key names, the value of the last duplicate will \
    be in the resulting dictionary.
    Also, the tags will have any spaces replaced with underscores and be set to lowercase.
    """
    result = {}
    lines = metadata.split('\n')
    for l in lines:
        key_value = l.split(':', 1)
        if len(key_value) == 2:
            key = key_value[0].strip().replace(' ', '_').lower()
            value = key_value[1].strip()
            result[key] = value
    return result