As Dallas braced Saturday for the funerals of five officers killed in the deadliest attack on 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 Thursday night 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 polarization that once existed,” said Rep. John Conyers Jr., D-Mich., who in 1987 was so outraged by a spate of controversial police shootings in Dallas that he conducted a congressional inquiry into the killings.

A LONG TIME COMING

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 $8 from a vending machine at a nearby gas 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 neighborhoods 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 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.” 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 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.”

CITIZEN COMPLAINTS DROP

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 64 percent, 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 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, its 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 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 $1.1 million to settle a civil lawsuit.

Dallas police fatally shot five people in 2015, according to a Washington Post database. 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 armored 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 burglarized 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 Rev. Ronald Wright, 58, who runs Justice Seekers Texas, a Dallas activist group.

Local officials stress that it’s important to recognize the progress Dallas police have made. At the same time, they say, it’s important to recognize 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 Thursday night’s attack.

“The fact that it happened in Dallas? It’s just where it happened.”