Marriage may have saved prehistoric humans and helped us outlive the Neanderthals, as our Stone Age ancestors were smart enough to know not to have sex with their brothers and sisters and how to avoid inbreeding.

A team of scientists did a genetic analysis on human remains that dated back 34,000 years ago, finding that while they were alive, these people operated in much the same way as the hunter-gatherers of today: They purposely hooked up with partners who were not close family members using their broader social networks. A study in the journal Science explains that although the remains came from a small population group, their genes showed “limited” levels of inbreeding and relatedness. The scientists say that these prehistoric people probably used their social connections beyond their core groups to find mates, knowing the dangers of inbreeding.

“Our findings suggest that [upper paleolithic] social organization was similar to that of living [hunter-gatherers], with limited relatedness within residential groups embedded in a larger mating network,” the authors wrote.

The four prehistoric human skeletons that the scientists studied came from the Russian archaeological site Sungir, which is east of Moscow. They represent anatomically modern people from the late Stone Age, a time period when our ancestors were migrating out of Africa and into Europe and Asia for the first time, and they lived around the same time before being buried together.

After studying the genetics of the Sungir group, the researchers compared it to the genomes of other humans from both ancient and modern times.

According to the study, the information about their mating practices could help pinpoint when hunter-gatherers developed the social networks that we still see today.

“Our distant ancestors are likely to have been aware of the dangers of inbreeding, and purposely avoided it at a surprisingly early stage in prehistory,” the University of Cambridge said in a statement.

The people studied were no closer than second cousins.

Along with creating larger social networks that would help them avoid inbreeding, these prehistoric humans may have also developed the earliest marriage traditions — precursors to those that became commonplace later on and evolved into the ones we still have now.

“The symbolism, complexity and time invested in the objects and jewellery found buried with the remains also suggests that it is possible that they developed rules, ceremonies and rituals to accompany the exchange of mates between groups,” according to the university. Those traditions might have “foreshadowed modern marriage ceremonies.”

The marriages and strategic choices of mates could be one reason Homo sapiens are still alive today and other prehistoric human species, like the Neanderthal, are not, the university explained. They might have lived in smaller communities of a couple dozen people but branched out for mates into groups that could have had a couple hundred.

“If small hunter–gatherer bands were mixing at random, we would see much greater evidence of inbreeding than we have here,” senior author Eske Willerslev said in the statement.

DNA from Neanderthals, on the other hand, has revealed levels of inbreeding. The university gives the example of 50,000-year-old Neanderthals in the central Asian Altai Mountains who showed signs of inbreeding, perhaps because they became isolated from a larger community of potential mates or because they had never formed such a larger network to begin with.

The prehistoric human mating strategies that stopped inbreeding may not have included rules against interbreeding — evidence has suggested that ancient Homo sapiens mated with Neanderthals quite a bit.