As Dallas braced for the funerals of five officers killed in the deadliest attack on US police in nearly a century, their heartbroken colleagues were left to ask: Why here?
According to police, gunman Micah Xavier Johnson told negotiators that "he was upset about the recent police shootings," but Dallas officers hadn't fatally shot anyone in 2016.
Johnson said he was "upset about Black Lives Matter," but the police chief in Dallas, who is black, is a national leader in the effort to reduce officer-involved violence.
"We don't have a perfect department. But if you want to look at our peers, cities of over a million people, we very well can claim to be number one," said Philip Kingston, the city council member who represents the downtown district where Johnson targeted white officers on Friday before being killed by police.
Civil rights activists, national police leaders and Dallas officials say the department is a model of the very reforms sought by the Black Lives Matter movement since a white officer shot and killed a black teenager in Ferguson, Missouri, in August 2014.
Although Dallas police have a dark history of brutality against minorities, two decades of fresh leadership have transformed the department's reputation and dramatically improved relations with the community.
Just before Johnson opened fire, Dallas police had been mingling peacefully with protesters marching to express concern about fatal police shootings last week in Minnesota and Louisiana, posing for photos the department tweeted out on its official account.
"The city leadership and the police and the community don't have the racial polarisation that once existed," said Congressman John Conyers, who in 1987 was so outraged by a spate of controversial police shootings in Dallas that he conducted a congressional inquiry into the killings.
The department's journey has been a long one. Humiliated by the assassination of President John F Kennedy on their watch in 1963 - and the subsequent murder of his accused assassin in the belly of police headquarters - Dallas police also had a notorious record of brutality against blacks and Hispanics.
In 1973, Santos Rodriguez, a 12-year-old Hispanic boy, was pulled from his home by officers who accused him and his 13-year-old brother of stealing US$8 from a vending machine at a nearby service station. When they refused to confess, officer Darrell Cain played a game of Russian roulette with the handcuffed boys, loading a single bullet in the chamber of his .357 Magnum.
The bullet fired, and Rodriguez was killed.
The shooting sparked days of protests. Although Cain was convicted of murder, he served just 2 1/2 years in prison.
"It was the worst police shooting to ever happen in the history of the city of Dallas," said David Kunkle, a rookie patrol officer at the time who later served as Dallas's police chief.
Tensions heightened when crime spiked during the crack cocaine epidemic of the 1980s, and police adopted more aggressive tactics, particularly in the black neighbourhoods of South Dallas.
"The late 1970s and 1980s were bad years for everyone," Kunkle said.
Another notorious shooting occurred in 1986: A Dallas police officer shot and killed Etta Collins, a black Sunday-school teacher who called police because she thought she heard a burglar. A rookie officer arrived to find Collins, 70, on her front porch with a revolver. He fatally shot her, he said later, because he thought she was aiming the gun at him.
Collins was one of 10 people fatally shot by Dallas police that year, six of whom were black, according to an analysis by the Dallas Times Herald, which concluded that Dallas police were killing more people per capita than any department in the nation.
The Chicago Tribune declared in 1988 that the city "a racial time bomb." Violent confrontations with police devolved into targeted killings: Five Dallas officers were killed that year alone.
"The police were angry at the community and the community was mad at the police," said John Creuzot, a former prosecutor who secured the death penalty for one of the officers' killers.
Creuzot, who is black, said things began to improve in the 1990s. The city elected its first black mayor, Ron Kirk, in 1995, and Kirk hired a series of outsiders with previous experience at reforming police departments.
Among the changes: a shift in the department's use of force policy. Officers were told that if they killed someone, investigators would examine all of their actions and tactical decisions rather than simply assessing whether they were in reasonable fear for their lives at the moment they pulled the trigger.
"There was a refocus on training and then a focus on the use of deadly force, and things seemed to change," said Creuzot, who served for two decades as a state district judge on the Dallas County criminal bench.
"We focused on community policing, and now we're focusing on data-related policing," he said. "Over time, the tension went down and the trust went up."
Things have continued to improve under Dallas Police Chief David Brown, who took command in 2010. Between 2009 and 2014, citizen complaints of excessive force by Dallas officers dropped by 64 per cent, according to police department data.
"Chief Brown understands that when you orient around the democratic values that are supposed to undergird law enforcement, you make your entire city safer," said Phillip Goff, president of the Center for Policing Equity, which promotes police transparency and accountability.
Community leaders and police reform activists caution that Dallas police still have room for improvement. In 2004, the city fired Terrell Bolton, it's first black police chief, in part because of a scandal involving the use of fake evidence in drug cases. More than 80 cases were dismissed, many of them involving false charges against Mexican immigrants.
In 2009, several Dallas officers were captured on a dashboard camera beating and kicking Ronald Jones, a black man who had been walking down the street. Jones spent 15 months in jail on charges of assaulting a police officer before the video was released. The city later paid Jones US$1.1 million to settle a civil lawsuit.
Dallas police fatally shot five people in 2015, according to a Washington Post database of such shootings. The department has yet to have a fatal shooting in 2016.
Among those killed last year was James Boulware, a 35-year-old white man who shot at Dallas police headquarters from an armoured vehicle laced with explosives.
But Dallas police also killed two black men, including 45-year-old Desmond Luster. According to police, Luster drove up in a pickup truck and opened fire in February on a police officer who had detained a man who Luster thought had robbed his home. Luster's family has sued, claiming the officer opened fire.
"Dallas still has some terrible shootings, and Dallas police still do some terrible things," said the Rev Ronald Wright, 58, who runs Justice Seekers Texas, a Dallas activist group.
Wright also noted an incident last year in nearby McKinney, where an officer tackled a young black girl in a bikini and pulled a gun on two black boys at an unruly pool party. Video of the incident went viral.
"I don't know why the Justice Department doesn't investigate all of these departments," Wright said.
Local officials stress that it's important to recognise the progress Dallas police have made. At the same time, they say, it's important to recognise that officers everywhere may be at risk until police leaders find a way to reduce incidents of excessive violence across the nation and ease rising racial tensions.
"With the number of police shootings that have occurred that seem to be totally unjustified, somewhere in this country someone was going to do such a thing," Creuzot said of Friday's attack.
"The fact that it happened in Dallas? It's just where it happened."