Brazilian authorities are scrambling to stop a wave of prison violence that has killed at least 125 inmates in two weeks, many with their heads cut off or their hearts and intestines ripped out. Three massacres and many smaller fatal clashes, all in northern states, have followed a pattern that security forces have been unable to get ahead of. It starts with an hours-long riot inside a penitentiary, then military police are sent in to assist severely outnumbered guards and distraught families wait outside while inmates post on social media photos they take of chopped up body parts. The factors fueling the violence among rival gang members are so complex that authorities say some potential remedies could exacerbate the problem. And the possibility of continued killings is raising fears the violence could spill into favelas, or slums, in major cities like Rio de Janeiro and Sao Paulo, where drug operations are run in conjunction with bosses in prisons. "Stopping the bloodshed begins with the federal and state governments calling this what it is: a public emergency," said Robert Muggah, research director for the Igarape Institute, a Brazil-based think tank that focuses on security. "This is a problem from hell that has been festering for half a century." President Michel Temer was initially slow to respond, taking three days to publicly acknowledge the first massacre, and he was ridiculed for calling it a "terrible accident." But the government has since announced a series of measures aimed at stopping the violence. National guard troops are being dispatched to hot spots, thousands of inmates are being shuffled among prisons and Temer has announced plans to build five new lockups. Meanwhile, justice authorities are examining ways to speed up a legal process that keeps thousands locked up for months or years while their cases are processed. Nothing seems to be working so far. After several days of relative calm, 26 inmates were killed late Saturday in clashes inside two adjoining prisons in the northern state of Rio Grande do Norte. Authorities said members of the Sao Paulo-based First Command, Brazil's largest criminal gang known by the Portuguese acronym PCC, fought with local gang Crime Syndicate of Rio Grande do Norte. Authorities said military police would not enter the prisons until daylight Sunday because it was too dangerous. "There are more than 1,000 prisoners, all out of their cells, and some are armed," military police Maj. Wellington Camilo da Silva told reporters. "What we are doing now is trying to avoid a massive escape." Prisoners returned to their cells by Sunday, and police found 26 bodies, many of them decapitated. The wave of violence began with the arrival of the New Year, when 56 inmates were killed in the northern state of Amazonas. Authorities said the Family of the North gang targeted members of the First Command in a clash over control of drug-trafficking routes in northern states. Many were decapitated, their heads tossed outside prison walls.