Let’s back up a minute. Why were egg producers ever grinding up newborn chicks to begin with? Basically, because they’re useless to the egg industry, explained David Coman-Hidy, executive director of The Humane League , a farm animal protection group that negotiated the agreement.

United Egg Producers , the industry group that represents 95 percent of egg producers in the country, announced Thursday that they would end the process of “culling” male chicks by 2020. Instead, they’ll use technology that determines the sex of a chicken embryo still inside an egg.

Most egg farmers in the United States will stop grinding male baby chickens to death over the next four years.

Daleen Loest via Getty Images An industry group representing most egg producers in the U.S. has pledged to end the practice of mass-slaughtering male chicks.

Egg farmers don’t sell off the chicks to be raised for meat, because in industrial agriculture, the breeds of chickens raised for meat and for egg production are totally different.

“Egg layers are bred to be egg-laying machines who can pump out hundreds of eggs each year. They’re expected to live for approximately one year, typically confined to a tiny cage,” Coman-Hidy told The Huffington Post in an email. “On the other hand, broilers are bred to be grotesquely large, growing to a huge size within one month — this is like having a toddler that weighs hundreds of pounds — when they are slaughtered. This leaves the male laying breed as the odd man out — too small to be really profitable to raise for meat and unable to lay eggs.”

The exact lifespans of broilers and egg hens varies. The U.S. Poultry and Egg Association says hens “usually” have a two-year “production cycle.” The U.S. Department of Agriculture puts typical broiler slaughter age between 38 and 61 days.

Until now, the industry’s solution to deal with these millions of unwanted male chicks has been to throw them, typically still alive, into high-speed grinders similar to woodchippers. Sometimes instead, the chicks are suffocated, or killed via their spinal cords being severed, according to a Humane League news release about the decision.

But now United Egg Producers is pursuing an alternative called ovo-sexing, which involves sticking a needle into an egg to detect slight differences in DNA to indicate whether the hatched chick will be a male or a female. Eggs that will hatch into males can then simply be put into the egg supply chain, Coman-Hidy said.

The reason this might take until 2020 is because “the technology is not commercially available yet — but it will be soon,” according to Coman-Hidy.

Chad Gregory, United Egg Producers president and CEO, said in a press release that the group will adopt the technology “as soon as it is commercially available and economically feasible.”

While the change will spare millions and millions of male chicks from a painful death, the sad truth remains that the surviving hens largely go on to lead miserable lives in cages so small the animals can barely move. “Cage-free” eggs are a step in the right direction, but a cage-free label is no guarantee that eggs are cruelty-free. “Cage-free” hens often still live in crowded barns with no windows or stimulation.

But Coman-Hidy is optimistic that these changes represent a major shift for the better in the way that people think about the animals humans use for food.

“It’s an indicator that society is coming to recognize that farmed animals have lives, they feel pain and that they matter,” he said.

CORRECTION: A previous version of this story referred to many barns for egg-laying hens as “dark.” They are typically artificially lit. Language has also been added to clarify the life expectancy for broiler and egg chickens.