In a surgery worthy of the most convoluted Grey’s Anatomy plot, surgeons at California Pacific Medical Center in San Francisco completed a two-day, six-way kidney transplant late last week. Five surgeons and dozens of anesthesiologists and nurses daisy-chained 12 patients together in the West Coast's largest paired transplant ever.

Patients who need a new kidney are often subjugated to years-long waiting lists, biding their time until an organ becomes available, typically from a recently deceased donor. But living transplants, from donors who give a kidney to a relative or friend, are far more likely to succeed—and can last twice as long before another transplant is needed. The problem is, that selfless donor's kidney might not be a solid match for its intended recipient. That's why transplant centers are increasingly working to build chains of donors and recipients: Pairing those who would have donated to a friend with a stranger, so everybody gets the kidney that's right for them. And with more than 100,000 people on the kidney waiting list, a chain of paired donations can have a far greater impact on that backlog than a single, closed-loop swap would.

On Friday, surgeons successfully completed a six-way paired kidney exchange at California Pacific Medical Center. Sutter Health/California Pacific Medical Center

Today, about a third of the 16,000 annual kidney transplants come from living donors, a number that keeps rising through paired procedures. Getting six people to donate their kidney to a complete stranger is a remarkable feat of altruism. But it’s also a testament to biomedical technology: The testing and treatments that make kidney transplants so successful, and the algorithms doctors and patients use to find the right match—or, in this case, six matches.

This specific surgery was facilitated by software called MatchGrid, created by former kidney recipient David Jacobs (Jacobs, coincidentally, was once a WIRED editor). To be a match, a donor and a recipient need to align on several medical fronts, including age, weight, and a laundry list of biological markers. The closer the match, the less likely a recipient is to reject the new kidney. In about 15 percent of transplants, the body’s immune system turns on the foreign organ and destroys it within a year.

To find the best pairings possible, MatchGrid pulled in data from a pool of potential donors and recipients in the Sutter Health network in the Bay Area, churning through their medical variables to isolate every possible compatible pair.

The first variable to match is obvious: blood type. The four types—A, B, AB, and O—have different rules about who can donate to whom, based on the immune system-stimulating antigens that appear on the surface of red blood cells. In this case, the donor who kicked off the chain was special: She was type O, which meant she could donate to anyone. That opened up far more transplant opportunities than otherwise might have been possible. “When we inserted this blood type O non-directed donor into the program, 140 different chains popped up,” says Steven Katznelson, medical director of the kidney transplant team.

From those 140 potential chains of patients, the software winnowed its set of participants based on patients' other antigenic signatures. MatchGrid includes 14 of those antigens—specifically, human leukocyte antigens—in its matching calculations. Those are markers that appear on the surface of immune cells, commonly used to determine compatibility between organ donors and recipients. Ideally, donor and recipient will express exactly the same set, since any unfamiliar antigen will invoke an immune reaction, potentially contributing to transplant rejection. Unless the transplant is between identical twins, that's almost impossible, though, so MatchGrid builds its chains to create as much overlap as possible.

MatchGrid also accounts for thousands of natively present antibodies that can influence whether an organ takes. Over time, someone waiting on the transplant list is exposed to many foreign proteins, whether they come in the form of a blood transfusion, a previous organ transplant, or even a pregnancy. Patients develop antibodies against those proteins, and once they’re in the system, they don’t go away—meaning they’ll be more likely to attack foreign proteins from a new transplant.

That's what happened to one of the recipients in last week's epic chain. With dozens of antibodies floating around, her pool of potential donors was limited to just 10 percent of the population with her blood type. Long odds indeed. But a combination of immunosuppressive therapy and the MatchGrid software—which pulls in medical records that show each of the antibodies present, in addition to how strongly they're expressed—made it possible for her to receive a kidney. “The software pulled in all her antibodies, and excluded all those donors against which she would have a reaction. It excluded the wrong blood type, the wrong genetic fingerprinting, and the wrong antibodies,” says Katznelson.

With Jacobs' software—and others used by the National Kidney Registry and the United Network for Organ Sharing—it's possible to string together as many as 10 pairs of kidney donations at a time. "But it’s not good enough just to say how many possible matches there are,” says Jacobs. "You have to learn what the best possible matches are." The software’s algorithm becomes one of optimization—choosing the greatest number of kidney transplants that can possibly occur at the same time, while still finding the most immunological commonalities between pairs.

Jacobs soon hopes to spread the use of his software around the country, letting small networks of hospitals like California Pacific Medical Center pull off their own chains. The Kidney Registry and United Network can do the same thing, but since their chains run between multiple transplant centers, they often fall apart before the surgery actually happens. Several of Katznelson's patients have gone through the heartache of seeing their chains break down. With better matching, more of them will end up, like the six recipients recuperating in San Francisco, with a different kind of ache—somewhere in the lower abdomen, with a fresh incision just above their brand-new kidney.