Aha! I figured out what was bugging me. My preprocessing has to be done in the correct order. First I have to search the INDI and FAM records for references. These are usually references to structures. Then I have to search the structures for references. These may be links back to other structures or links back to INDI and FAM records. The difficulty is that this can be recursive with links calling other links. For example, an Event may reference a Citation that is part of a Source. The Citation may refer to a Repository. The Repository may refer to a Note. The Note may refer to a Source, which could be the same one or a different one.
The thing I was doing wrong was in order to sort the structures by title, I determined the titles before I computed the references. However, some references were not calculated yet, so they had no titles. Also, the recursion of sources was not handled correctly. And the References were not properly attached as a result.
Finally, if selecting only part of a family, what is obviously desired is to only include Notes, Places, Sources, Repositories and other structures that are referenced by the people included. This could not be done the way it was set up, because the chain of references needs to be followed through to ensure that there are references to included people.
If this all sounds confusing, that’s because it is. And this was causing me my troubles trying to figure out what was wrong with the recent changes. I had better fix these as soon as possible, because I’m sure I’m going to come back in a month from now and read this again and be very confused by it.