For an egg farm, male chicks are no more than a nuisance. They don’t lay eggs, of course. And egg-laying breeds don’t grow big enough to sell for meat. So according to the cold, hard economics of the factory farm, male chicks are ground up or gassed soon after they hatch—hundreds of millions a year, according to the farm animal advocates at the Humane League.

That grisly practice may end soon. United Egg Producers, which represents almost all of the country’s egg farms, recently announced it will stop culling male chicks by 2020. It will instead look into high-tech methods that can ID an egg as male or female just a few days after it’s laid, also known as in ovo sexing. But nobody has an industrial scale machine that can sex millions of eggs; the technology is still young.

The speed at which the US industry has agreed to end culling is unusual. Another campaign, for cage-free eggs, has been a success—but it was long, expensive, and obviously not great PR for the egg industry. “We had a tense relationship with United Egg Producers,” says Humane League executive director David Coman-Hidy. So when it came to the issue of culling, the Humane League decided it could be more direct: It went straight to United Egg Producers and asked to work together. A few months later, the collaborators came out with their announcement to end culling of male chicks. (United Egg Producers is not giving interviews on the topic.)

One reason why the egg industry would have been more willing to play ball in this case? Going cage-free massively complicates the logistics of an existing egg farm—but sexing eggs might streamline it. Sexing chicks requires humans, expertly trained humans who pick up each chick and squeeze out its feces until its genitalia become visible. The difference is very subtle. And the whole thing is surprisingly labor-intensive.

If farms can identify a male egg just a few days after laying, they don’t have to pay to incubate all those male eggs for 21 days before they hatch. The eggs are fertilized, so they won’t end up in the supermarket. But they can be diverted elsewhere, like for making flu vaccines, which use up some 100 million fertilized eggs per year.

How to Sex an Egg

That’s all assuming farms can reliably sex eggs thousands at a time. Two groups in the Netherlands and Germany have competing ideas for the best way to do it.

At the University of Leipzig in Germany, a group has been studying chicken sex chromosomes for the past decade. In birds, the male chromosome is slightly larger than a female chromosome. So the group uses a laser to cut out a hole less than a quarter-inch wide in the shell, shining infrared light onto the web of blood vessels inside. Based on how the light scatters, they can figure out whether the chick has male or female chromosomes. Shining and analyzing takes about 15 to 20 seconds per egg. Maria Krautwald-Junghanns Elisabeth, who heads up the project, says she expects to demonstrate the technology to the German government by summer 2017.

Gottschalk/photothek/BmEL

A Dutch startup called In Ovo has another idea that uses chemistry instead of light. In Ovo takes a small needle—from a machine used in the vaccine-making process, actually—to extract a tiny bit of allantoic fluid. “It’s the waste bag for the embryo. It looks a bit like urine.,” says cofounder Wouter Bruins. They run the sample through a mass spectrometer, which vaporizes the egg fluid and looks for one specific molecule. Bruin wouldn’t divulge which molecule it is, but he said his company’s scientists can detect sex differences by day nine with 95 percent accuracy1. The mass spec test takes four seconds per egg.

The first batch of eggs they tested actually just hatched about three weeks ago. The company has partnered with the four largest Dutch hatcheries as well as Sanovo Technology Group, which makes egg handling machines.

Once the technology for detecting sex differences is reliable, integrating it into the egg farm will actually be pretty easy, says Bruins. “Hatcheries now are quite automated,” he says. Machines already pick up and sort out unfertilized eggs by shining light through them (It’s called candling, a name that invokes the old days of farming). Sexing the eggs is just a matter of adding a test and another round of sorting to the system. The initial cost of egg sexing machines might wipe out any savings, but it’ll be worth it for an egg industry looking to burnish its ethical reputation.

Like many things, the technology to sex eggs exist in a lab, but it needs a nudge to become commercially viable. It needs, you might say, a startup incubator. The United Egg Producers’ commitment will certainly help.

1 UPDATE 6/21/16 11:40 ET: This story has been updated with the correct number of days before In Ovo can sex chicken eggs.