For performance reasons, we shouldn't guess the doc type if it's already known --> I think a "if doc is None:" is missing here