One morning in this grim farming town, a Mexican judge who carries a rubber-bullet gun for protection strode into his courtroom to consider the matter of the 11-inch knife.

Slumped at the defendant’s table was David Ramos, a day laborer charged with attempted homicide for participating in a drunken knife fight. Ramos had already spent 16 days in jail. But Judge Juan Antonio Rubio Gutiérrez had discovered a glaring irregularity.

In the initial paperwork, no one mentioned where police found the plastic-handled blade. When the point had been raised, the missing information suddenly appeared in a new shade of blue ink. Rubio Gutiérrez decided that the information was dubious and that the defendant could walk.

“Procedurally speaking, a knife no longer exists,” the judge told Ramos in the courtroom earlier this month. “Today, you have recovered your freedom.”

The scene playing out in this new one-room courthouse represents a radical departure from the old Mexican traditions of law and order.

Mexico is completing its first full year of a new accusatory justice system, following the most profound overhaul of its legal structure in a century. The most visible sign of the transformation is public trials instead of a secretive process involving written arguments. But the changes go far deeper. Both Mexican and U.S. officials have described the system as crucial to restoring order to a country torn apart by drug violence.

So far, the results have been chaos.

Aldo Monjardín, a police commander in Guadalajara, inspects a backpack. Some officials say the country’s new justice system has made security forces weaker, with criminals being set free because of paperwork errors.

Bickering and confusion reign at each link in the legal chain. Police complain of hours lost on laborious forms; prosecutors blame judges for setting criminals free; judges accuse poorly trained police of botching crime scenes. Powerful drug cartels, meanwhile, are exploiting the weaknesses in the new system and strong-arming authorities with death threats and bribes.

The upheaval has come during the deadliest year in Mexico’s modern history. Politicians here increasingly blame the judicial changes for emptying jails and fanning crime. Even those who embrace the new legal system worry about its first-year fiascos.

“The reform is going badly,” José Ramón Cossío, a justice on Mexico’s Supreme Court, said in an interview. “There are many small problems that, taken together, are causing what I believe to be an important crisis.”

It is hard to overstate the significance of the restructuring. It seeks to turn the notoriously ineffective police into professional investigators. It strengthens the independence of judges. It provides more rights to defendants in a country where authorities have been known to demand bribes, extract confessions under torture and doctor evidence.

The U.S. government is deeply invested in the project, contributing more than $300 million since 2008 to equip courthouses and train police and legal personnel.

Even in rural outposts such as Ocotlan, the system has ushered in many trappings of high-tech justice: courthouses with surveillance cameras and fingerprint sensors; forensic investigators at crime scenes in latex gloves and protective footwear.

But the exacting new procedures have been grafted onto feeble, corruption-plagued institutions created decades ago by an authoritarian state.

Judges are demanding the kind of legal precision found in Washington or London, from police who sometimes can barely read and live in places that can feel like war zones.

“This is a baby that has just been born,” Rubio Gutiérrez said in an interview. “We are asking the system to run, and it is not possible.”

‘We are weak’

A Virgin of Guadalupe banner flies above the streets of Jamay.

The western state of Jalisco is home to the most dangerous drug cartel in Mexico, a network of traffickers and assassins who have shot down an army helicopter, ambushed federal police and sent a pig’s head to the former attorney general’s home. Cartel Jalisco New Generation represents the ultimate test of the fledgling legal system.

This year, crime has been winning. The state has recorded 1,218 homicides through November, putting it on pace for its deadliest year in the past two decades of available statistics. In Ocotlan, home to many cartel gunmen, traffickers and police have clashed. Not far away, bodies have been discovered in mass graves.

It was in this unnerving atmosphere that Rubio Gutiérrez began his job last year in the state’s fourth judicial district.

A youthful jurist with a quick stride and confident air, Rubio Gutiérrez, 37, was quick to embrace the new system. He wrote a 385-page book about it. He has opened an institute to teach lawyers about the big legal shift underway: from written proceedings to oral trials, with an explicit presumption of innocence.

The first person in his family to graduate from college, Rubio Gutiérrez began as an unpaid courthouse aide. As he rose through the judicial ranks, he witnessed a system in meltdown. Crime was soaring, judicial backlogs were massive and only a tiny fraction of crimes ever resulted in convictions.

Birds fly near the town of La Calera in Jalisco state. Jalisco is home to the dangerous New Generation cartel. Judge Juan Antonio Rubio Gutiérrez holds classes to help lawyers adjust to the legal shift underway.

At the same time, penitentiaries were flooded with people caught carrying guns or small amounts of drugs. Their cases could drag on for years before they were sentenced.

“There were many injustices,” Rubio Gutiérrez said.

Now judges have far more leeway to release suspects pending trial. The new system provides alternatives such as mediation or plea bargaining to ease the congestion in the court system.

The result has been fewer people behind bars. Mexico has about 202,700 prisoners, down from nearly 235,900 when the changes went into effect in June 2016, according to prison authorities. Mexico City Mayor Miguel Ángel Mancera said last month that there are 11,000 fewer inmates in the capital than in the year before the judicial revisions started — a decline of nearly 30 percent — a situation he called “very dangerous.”

Judges now have greater power to toss out charges when a suspect’s rights have been violated. Rubio Gutiérrez and many other judges blame the high number of suspects released on errors by poorly trained police and prosecutors. Often these are paperwork mistakes by police unaccustomed to the new 22-page incident report that is required for every arrest or crime scene. The chain of custody for evidence is regularly violated.

One recent afternoon, Rubio Gutiérrez drove to Ocotlan’s neighboring town, Jamay, to lecture the local police force about how to avoid errors and document their cases.

“I’m not a mind reader. I’m a judge,” he told them. “Help yourselves out.”

The police listened respectfully. But a few days later, their police chief, Fidel Moreno Robledo, sat in his cramped office and laid out the reality of a small rural force.

Of the 16 officers theoretically available on any shift, several are detailed to guard government buildings, while others are often injured or on vacation, leaving fewer than five able to patrol a municipality of 25,000 people, he said. His men get paid $400 per month and receive no life insurance or social security. All this, in a town where last year police recovered 20 bodies floating down the Lerma River, one of the many drug-war front lines in Mexico.

“We are weak,” Moreno said.

And the new system, he said, has made them weaker.

About 20 policemen have been fired for failing the national background tests intended to weed out corruption. Now, police can’t enter houses as easily without a warrant, which are often hard to get. Suspects have the right to remain silent; police must justify stops and searches. If there is the “smallest error” in paperwork or a delay getting a detainee before a judge, Moreno said, a “criminal, a kidnapper, a killer, gets set free.”

A period of ‘great confusion’

Guadalajara police speak with residents. In the latter half of this year, the city’s crime rate has more than doubled over the rate in the first half of 2016, before the new judicial system was launched, says Police Chief Salvador Caro Cabrera.

The push to overhaul Mexico’s legal system began a decade ago as violence flared across the country. Former Mexican president Felipe Calderón had declared war on drug cartels in 2006, and the death toll began to mount.

The old legal structure couldn’t cope with the bloodshed. It was based on the inquisitorial system, also used in other parts of Latin America, but it was shaped by the authoritarian, one-party system that defined Mexico for most of the 20th century. Police were often seen as an instrument of control — not investigation. Judicial appointees, meanwhile, were expected to be loyal to the ruling Institutional Revolutionary Party. Judges rarely disagreed with the written cases put together by prosecutors.

The deadline to adopt the accusatory system was June 2016. Many states waited until just months out to start the shift. Federal and state governments spent a fraction of what was required, according to Héctor Díaz Santana, the former head of the organization in charge of implementing the changes.

“We have poorly trained, unprofessional police, poorly paid prosecutors accustomed to the old ways, judges that were very comfortable before because you never saw them,” he said. “They created a very demanding system when we practically don’t have the tools.”

A man who was shot is led to an ambulance. Guadalajara’s police chief says there are 1,300 crimes here per month. An investigator looks for spent casings. Before the new system was introduced, police in Mexico were often seen as an instrument of control.

When Salvador Caro Cabrera took over as Guadalajara police chief in October 2015, only 80 of his 2,600 policemen had received any training on the new protocols for collecting evidence, writing up crime scenes or interacting with prosecutors.

“We have had a period of great confusion,” he said.

In the latter half of this year, the Guadalajara crime rate has more than doubled over the rate in the first half of 2016, before the new judicial system began, Caro Cabrera said. Under the old system, he said, more than 100 people arrested each month went to prison; now only 10 to 15 end up in jail.

The chief said only 50 arrest warrants have been issued in Guadalajara, the state capital, in the past year and a half — while there are 1,300 crimes per month.

“The judges are a disaster,” Caro Cabrera said.

The judges have their own concerns. The accusatory system is far more transparent, with prosecutors and defense attorneys arguing in public hearings, as in the United States. But that can be unsettling, even to defenders of the changes, like Rubio Gutiérrez.

Unlike the old system, in which judges signed off on mountains of paperwork behind closed doors, Rubio Gutiérerez sits behind a blond-wood bench at hearings and looks the suspects — and the public — in the face.

“It’s much more dangerous. You are in front of the criminals,” he said.

One day last month in Guadalajara, a cooler containing body parts was placed outside a courthouse. A note warned a judge: “You’re next.”

Because it’s difficult to get a weapons permit, Rubio Gutiérrez bought an “Angel Guardian” rubber bullet pistol. Earlier this year, someone hurled from the street a wrapped-up knife that bounced off his office window.

“We don’t have protection, guns, nothing,” he said.

Calls for revisions

Bus passengers look at the crime scene where Luz Margarita Ramirez Gallardo was shot to death in Guadalajara on Dec. 5.

In many ways, the crime scene seemed like something out of an American cop show. A woman slumped dead in her white van. Municipal police strung up yellow tape and filled out paperwork. State forensic staff in white jumpsuits placed numbered placards next to shell casings. State prosecutor staff questioned neighbors about the afternoon’s shooting.

“This is the same process as the United States,” said Jose Luis Estrada, a Guadalajara police spokesman on the scene. “This is all new for Mexico.”

Under the old system, most Mexican police had little role in investigations and were supposed to focus on preventing crime. The new protocols require them to rigorously process crime scenes.

But follow-up remains a glaring weakness as the system takes hold. And impunity remains high.

“The problem is not that people are getting out of prison,” said Guillermo Zepeda Lecuona, a law professor at the University of Guadalajara who is an expert on the judicial revisions. “It’s that they are not going in.”

The case of Luz Margarita Ramirez Gallardo, the ­35-year-old woman found dead in her van, shows how the new system still isn’t stopping crime.

Early on Nov. 2, just over a month before Ramirez was killed, two gunmen approached her as she was backing her van out of the garage in the working-class Olimpica neighborhood of Guadalajara. They told her to hand over the keys and then “they shot her,” according to her 18-year-old son, Jonatan Ramirez.

Ramirez was hit twice in the face and lost her right eye but somehow survived.

The police appeared to handle the crime scene professionally. But Ramirez’s family says that after the first day, police and prosecutors never asked them for more information about the shooting. No arrests were made.

Aldo Monjardín, a police commander in southern Guadalajara, questioned Ramirez in the hospital. He found her story of a robbery suspicious, he recalled; nothing had been stolen, including the van.

Monjardín noticed what he believed were breast implants, as Ramirez lay supine in the hospital bed. He assumed she was the girlfriend of some cartel figure and had crossed the wrong narco.

“Women love to go out with these guys,” he said.

Authorities denied they had shrugged off the investigation. An official from the attorney general’s office in Jalisco said the Ramirez family had not been forthcoming. The official, who spoke on the condition of anonymity because he was not authorized to speak publicly, refused to answer further questions about the case. Prosecutors say witnesses are often too afraid to talk, even more so now that proceedings are in open court.

Many prosecutors are also not used to assembling complex cases. In the past, they often relied on confessions from suspects — sometimes criminals caught in the act, sometimes people who admitted to a crime under ­torture.

“The new system is totally opposite” to the old, said Alejandro Torres Ramirez, 32, a prosecutor in Jalisco. “First you have to investigate and get proof together to be able to arrest someone, something that we’re not used to, culturally.”

Within a couple weeks, Ramirez was back home and working again. On the afternoon of Dec. 5, a man got out of a gray BMW, walked up to the passenger window of her van and shot her dead.

A resident watches an officer. Police must rigorously process crime scenes under the new protocols. Relatives of Ramirez place photos of her at a shrine.

A neighbor, who identified himself only as Hugo, said he had called the city’s emergency number at least four times about suspicious vehicles on the street in the two weeks leading up to the murder. “The police never arrived,” he said. The police said a patrol passed by the scene about 10 minutes before the killing but saw nothing suspicious.

The chaos in the new judicial system and rising crime rates in Mexico have prompted politicians to call for major revisions in the protocols; some even openly yearn for the old procedures.

Many judicial officials insist regressing would be disaster. They say the changes will eventually encourage more rigorous investigations and make Mexico's legal system more transparent and effective.

Those future benefits are of little consolation to the Ramirez ­family.

Some of her relatives assume the police who investigated her case were bought off by criminals, but Enrique Ramirez Gallardo, her eldest brother, doesn’t agree.

“I think they are just overwhelmed by all they have to do,” he said. “Unfortunately, what happened to my sister happens every day.”

Relatives of Ramirez comfort each other. Judicial officials say the new legal system will eventually be more effective and more democratic. But the future benefits are of little consolation to the Ramirez family.

Gabriela Martinez in Mexico City contributed to this report.