How does $cid_parts['entity'] get populated here?  I can't see how it gets into the cid_parts array.  Either way we probably don't want the entity serialized into the cid string.