IN 2005 José Antonio Zúñiga, a Mexican street vendor of computer services, was sentenced to 20 years in prison for murder. His conviction relied on the account of a lone witness, who did not mention Mr Zúñiga until his third statement to police. Other stallholders said he was at work during the murder. The court excluded statements supporting him, and ignored contradictions in the prosecution's case and a test that showed he had not fired the gun.

Mr Zúñiga had the good fortune to have two campaigning lawyers take on his case, who succeeded in overturning the verdict. In 2011 his story was featured in a documentary, "Presumed Guilty". But he is a rare exception among the wrongly convicted in Mexico. Overall, says an attorney in the film, "from the moment they accuse someone, the prosecution has won."

The message of "Presumed Guilty" would surprise most foreign observers of Mexico's drug war. The popular perception is that the country's courts fail to convict enough people. Around three-quarters of murders go unsolved, and the public has grown inured to the spectacle of masked soldiers parading recently arrested "traffickers" or "hit men" before the cameras, only to see them released days later.

But a hidden consequence of letting the guilty go free is that innocent people are often punished in their stead. Historically around 95% of criminal verdicts in Mexico have been convictions. And 90% of those have been based on confessions, which police have a nasty habit of beating out of prisoners. A study of 80 suspects arrested in connection with the killing of 43 student teachers in September 2014 alleged that 17 had been tortured. Separately, three police officers and two soldiers are facing torture charges after an online video showed a female suspect being asphyxiated with a plastic bag. Security experts generally say that only by safeguarding defendants' rights and building public trust in the justice system can the state hope to amass the evidence necessary to capture and convict the real culprits and deter organised crime.

A very long goodbye

Felipe Calderón, Mexico's conservative president from 2006 to 2012, is best known for deploying the country's army against its drug gangs. But he simultaneously took these arguments to heart by launching a root-and-branch transformation of its courts, which is scheduled to be fully implemented by June 18th. Miguel Ángel Osorio Chong, the interior minister, has declared it bids "goodbye to impunity".

The new system scraps the "inquisitorial" approach, in which a prosecutor presents written evidence that the defence has little opportunity to contest, in favour of a more transparent "adversarial" model, where lawyers argue their cases orally before a judge. It establishes basic rights for defendants, like the presumption of innocence and the provision of a lawyer, and excludes confessions from court unless a defence attorney was present when they were given. It allows alternative approaches to justice, such as mediation, for less serious cases. And it fights corruption by requiring the involvement of three separate judges: one to ensure the rights of the accused are observed before the trial, another to preside in court and a third to guarantee the sentence is carried out correctly.

The policy has been a long time coming. It became law in June 2008. When Mr Calderón left office in 2012, just under 30% of Mexicans lived in areas covered by the new rules. His successor, the centrist Enrique Peña Nieto, belongs to a different political party, but has proved an eager reformer. In addition to passing economic liberalisation that Mr Calderón supported but could not get through Congress, he increased federal transfers to the states to speed up the justice reform, and introduced a national penal code to ensure the uniform application of criminal law across the country. By June 7th 93% of Mexicans lived in regions where the new model has taken effect; the government says that figure will reach 100% by June 18th.

Evidence from states that have instituted the changes is encouraging. In particular, they seem to have streamlined the judicial process: the average time to resolve a case has dropped from 180 days to 34. In Mexico City, prison overcrowding fell by 70% in the system's first four months, mainly because many types of crime could be dealt with through mediation rather than by the courts. And three of the earlier-adopting states, Baja California, Morelos and Nuevo León, have reduced the share of defendants put in pre-trial custody—and thus housed next to convicted criminals—by around 20 percentage points. About 40% of Mexico's total prison population is awaiting trial, and the new availability of different bail measures (such as periodic reporting) and a presumption of innocence should grant many of them at least temporary freedom.

Nonetheless, Mr Peña will need to keep expectations in check now that the adversarial approach is in place. Even if implemented perfectly, it will not reduce crime on its own: security in Morelos, one of the first to set it up, has been deteriorating. The emphasis on challenging evidence in court means that police officers will have to get better at protecting a crime scene and preventing contamination. Corrupt officers will gain a new means of sabotaging legal proceedings, by mishandling evidence and claiming it was a mistake.

Moreover, the roll-out has been patchy. Chihuahua, in the north, instituted its own reform in 2007, before the policy was adopted nationwide, whereas Sonora, its neighbour, only did so recently. Many states that developed their own codes will have to adjust them to comply with new national standards. In some places the two systems will run in parallel, since crimes committed before the launch of the reforms will still be tried the old way. CIDAC, a think-tank, predicts it will take 11 years for the new model to operate effectively.

Yet despite such growing pains, there is wide consensus that the reforms are necessary if not sufficient to establish the rule of law in every corner of Mexico. Their implementation, says David Shirk of the University of San Diego, represents a "milestone in the marathon to a better criminal-justice system". That is reason for hope.