created generator to loop over unique ancestors, replaced write_ancestor_to_file code