def cleanup_classes(rdf):
    """Remove unnecessary class definitions: definitions of SKOS classes or
       unused classes. If a class is also a skos:Concept or skos:Collection,
       remove the 'classness' of it but leave the Concept/Collection."""
    for t in (OWL.Class, RDFS.Class):
        for cl in rdf.subjects(RDF.type, t):
            # SKOS classes may be safely removed
            if cl.startswith(SKOS):
                logging.debug("removing SKOS class definition: %s", cl)
                replace_subject(rdf, cl, None)
                continue
            # if there are instances of the class, keep the class def
            if rdf.value(None, RDF.type, cl, any=True) is not None:
                continue
            # if the class is used in a domain/range/equivalentClass
            # definition, keep the class def
            if rdf.value(None, RDFS.domain, cl, any=True) is not None:
                continue
            if rdf.value(None, RDFS.range, cl, any=True) is not None:
                continue
            if rdf.value(None, OWL.equivalentClass, cl, any=True) is not None:
                continue

            # if the class is also a skos:Concept or skos:Collection, only
            # remove its rdf:type
            if (cl, RDF.type, SKOS.Concept) in rdf \
               or (cl, RDF.type, SKOS.Collection) in rdf:
                logging.debug("removing classiness of %s", cl)
                rdf.remove((cl, RDF.type, t))
            else:  # remove it completely
                logging.debug("removing unused class definition: %s", cl)
                replace_subject(rdf, cl, None)