Image copyright Getty Images Image caption A sheriff's deputy in Harris County, Texas, responds to a fire at a chemical plant

Democrat Beto O'Rourke lost his Senate bid in 2018, but his failed campaign led to a major overhaul of the criminal justice system in Texas's largest city, writes The Marshall Project's Keri Blakinger.

The candidacy of O'Rourke, a young and charismatic congressman, energised Democratic voters in this deep red state and added momentum to a slowly-building blue wave in Harris County, where Houston sits.

The newly-victorious Houston-area Democrats - including 59 judges and the county's top executive - have tackled the hot-button issue of bail reform and are debating how to make sure that poorer people are represented by lawyers in criminal court.

Some new judges have recently changed longstanding courtroom culture by ending the shackling of juvenile suspects and fining prosecutors for withholding evidence.

Conservative Republicans, law enforcement officials and the bail bonds industry have loudly condemned those changes, but have not been able to reverse them.

The shift in Harris County is part of a broader rethinking of criminal justice across the US.

Long-time advocates against mass incarceration are being joined by celebrities like Kim Kardashian, so-called "progressive prosecutors" are gaining steam as they roll out new approaches to punishment, executions are declining, and both states and the federal government are continuing to roll back harsh drug sentences.

Image copyright BBC/Giles Edwards Image caption The county overseen by Democrat Rodney Ellis executes more people than any US state other than Texas

"For a very long time Texas was the death penalty capital," says Rodney Ellis, a longtime Democratic politician who's now a commissioner on the five-person board that runs the county. "Harris County was the epicentre of it, and there were legendary press stories about sleeping lawyers representing people on death row."

Since the US brought back capital punishment in the 1970s, the county has sent 129 men and women to the death chamber, more than any entire state except the rest of Texas.

Fifteen years ago, nearly a quarter of new prisoners in the state system came from Harris County. The number has since fallen by half and now accounts for only one in eight new prison admissions.

At the same time, the state and county demographics have shifted. Texas is now one of few majority-minority states in the nation - meaning that more than half the state population is made up of minority groups.

Image copyright BBC/Giles Edwards Image caption Cash bonds sold directly across the street from a courthouse in Texas

Image copyright Getty Images Image caption A Texas sheriff's deputy guards a university campus during a political event in 2007

In both the Houston area and Texas as a whole, growth in the Latino population - which tends to vote more Democratic - has outpaced the growth in the white population.

Amid those changes but "before Beto," Kim Ogg won her 2016 bid to be Harris County's top prosecutor. She's made some progressive moves, such as limiting marijuana arrests and sending fewer people to prison. But the local courts were still largely controlled by Republicans, many of whom opposed cutting back cash bail requirements.

That turned into a major issue in the 2018 race, as the county grappled with a class-action lawsuit originally filed on behalf of a young mother who'd been held in jail for driving without a licence and could not afford $2,500 (£2,000) bail. The lawsuit argued that the county's reliance on cash bail was unconstitutional because it amounted to a "wealth-based detention scheme," where rich people could pay for their release while poor people would have to stay in jail.

The county, and the 16 misdemeanour judges named as defendants, fought the lawsuit in federal court.

What is cash bail?

Bail allows suspects in criminal cases to go free as they await trial, so long as they or their friends and family pay a cash sum to the court

The payment is set by a judge, who considers factors such as the seriousness of the alleged crime and the risk that the suspect may flee

If the person appears in court as scheduled, their bail payment is returned at the end of their trial

Advocates of criminal justice reform say the practice is unfair to lower income Americans, who are jailed if they are unable to pay or enter into a plea deal with prosecutors that gives them a lesser punishment if they admit guilt

Then in 2018, Beto happened - and his momentum carried down the ballot. As a result, Democrats swept the judicial benches and won control of the county government.

Last year, the county and the new misdemeanour judges agreed to a settlement that largely eliminates cash bail for low-level arrests.

Image copyright BBC/Giles Edwards Image caption Judge Franklin Bynum describes himself as a "socialist"

"When I ran for office, I was really running to become a defendant in the lawsuit," said Judge Franklin Bynum, a self-described socialist who oversees a misdemeanour court. "Rather than fighting the lawsuit and spending all this money and hiring all these experts to say there's no problem, suddenly we all admit there's a problem."

Now roughly 85% of misdemeanour arrestees are automatically released without paying, and the county is beginning to grapple with another bail lawsuit focused on felony cases.

Under the new judges in the county's juvenile courts, the number of Houston-area children sent to youth prisons has fallen drastically, from 149 in the first 10 months of 2018 to 54 in the first 10 months of 2019.

At the same time, there's been a sharp rise in the number of criminal cases tossed out of court, as magistrates and judges scrutinising new cases are more frequently determining there's not enough "probable cause" evidence to move forward. In 2019, more than 3,200 cases were dismissed due to a lack of probable cause, a more than 70% increase from four years earlier.

Image copyright BBC/Giles Edwards Image caption Police union representative Joe Gamaldi calls some bail reforms "essential" but criticises other changes

Some of the changes - including to local bail practices - have already sparked criticism from the police union, whose outspoken leader has sparred with the new judges and district attorney in the past.

"There were parts of bail reform that were essential to have," said Joe Gamaldi, the Houston police union president. "But they're so enamoured with, 'Hey, we can do this!' that they don't stop to think if they should."

Will the changes in Houston's criminal justice system stick? Could they snowball in more reforms?

Answers could come later this year when the district attorney is up for re-election. She currently faces challengers from the right who want to roll things back - and from the left, pushing for more change.

This article was published in partnership with The Marshall Project, a nonprofit news organisation covering the US criminal justice system.

Listen to the BBC World Service documentary Houston, we have a new criminal justice system.