SOUTH BEND, Ind. — After Pete Buttigieg's eight years as mayor here, it's hard to say which has changed more — him or the city he's run.

When Buttigieg was elected in 2011, he was a closeted 29-year-old McKinsey consultant five years out of Oxford. By the time he announced his presidential run this month, he had come out as gay, served a tour of duty in Afghanistan and, like many millennials in their 30s, gotten married.

But more important, he had become a better mayor.

NBC News interviewed Buttigieg and more than 40 South Bend politicians, activists, law enforcement officials, real estate developers, voters, businessmen, clergy, community leaders and academics to better understand the evolution of the candidate who has shaken up the presidential race unlike any other.

Those who know him and have worked alongside him say Buttigieg, now 37, came into office thinking like a hired-gun consultant, full of big ideas and piles of numbers and studies to transform the city. With data-driven plans like those for "smart" sewers and "smart" streets, he began to awaken a city that had become resigned to its slow demise.

But gaps in his experience, especially with people from outside his rarefied world, increasingly became apparent. Even the best education in the world couldn't prepare the only child of two professors for the gritty realities of the job of running a diverse municipality.

A pedestrian walks along Main Street in South Bend.Joshua Lott / for NBC News

Still, as even his fiercest critics acknowledge, he listened, learned and took action, as he grew more confident personally and acknowledged the kind of shortcomings politicians often try to cover up.

He ditched the suit jacket in favor of his current uniform of rolled-up sleeves, and began to make a human connection with his constituents after an "aha moment" face to face with a young murder victim's mother.

"I came in really treating it as a management job," Buttigieg told NBC News in an interview. "I've come — kind of grudgingly — to realize that the management side of the job may be the lesser half."

After Buttigieg emerged victorious from a crowded Democratic primary and then trounced his Republican opponent, the promise and pitfalls of being South Bend's new mayor presented themselves almost immediately after he installed himself in his 14th-floor office in the County City Building on a snowy New Year's Day in 2012.

Good things started happening right away. Buttigieg went about remaking the creaky 1,400-person bureaucracy and attracting new development to a city (pop. 100,000) with a skyline dominated by a building whose penthouse once housed a swanky members-only club, but that had long since fallen into despair.

"The city had a reputation of telling you why you couldn't do something," said Jeff Rae, president of the South Bend Chamber and a former Republican mayor of neighboring Mishawaka. "It shifted more to, 'How we can get this deal done?'"

South Bend Mayor Pete Buttigieg near new construction at Ignition Park in 2012.Peter Hoffman / Redux

Some $200 million in investment poured into downtown and new condos, offices, parks, restaurants and cafes sprang up.

There was a new minor league ballpark and a farm team for the Chicago Cubs. A casino. A public light display on the river. New streets — "smart" streets — were redesigned as two-way thoroughfares with slower speed limits to help keep people downtown and boost retail businesses. Underneath the streets were "smart" sewers using electronic sensor technology to prevent overflow into the river.

A new warehouse and logistics park out by the airport created jobs, while the old Studebaker factory, the epicenter of South Bend's decline when it shut down in 1963, became a hub for data companies looking to tap into nearby fiber optic lines than ran under the train tracks.

"He was a young dynamic leader with a young dynamic team in place," said Tim Scott, president of the South Bend Common Council. "His team was just being aggressive and proactive, looking at data and looking at new urbanism, the latest and greatest trends in technology, community investment."

The prosperity that spread through downtown, however, never made its way to the city's low-income neighborhoods. A 2017 study commissioned by the city found that, amid a broad citywide decline in unemployment, the jobless rate among African Americans was still nearly twice as high as it was for white residents, and that 40 percent of the city's black community still lived in poverty.

And an inherited scandal — and Buttigieg's bungled response — exposed a racial rift in the city that would haunt his successes.

Just weeks after taking office, he learned that the city’s first black police chief, who had been appointed by a predecessor, was under federal investigation for authorizing a detective to secretly record telephone calls inside the police station in an attempt to expose racism among the disproportionately white force.

That appeared to violate federal wiretapping laws, so Buttigieg demoted the well-liked chief, Darryl Boykins, to captain.

But as many African Americans saw it, a black chief was being pushed aside for trying to fight racism. "It was meaningful to the African American community to have people in those kinds of positions," said Hardie Blake, a black pastor. "With Boykins, it definitely created distance."

Buttigieg was thinking like a consultant — fixing the problem by hiring as his new chief an outsider from Boston who was white — and who had a reputation for developing policing strategies based on data. The mayor wasn't thinking like a politician with a human touch.

In interviews with NBC News, city leaders and residents assessed "Mayor Pete" in similar terms: He was young. Lacked executive experience and a diverse staff. Too data-driven, too hurry-up. His administration was brash and didn't communicate effectively with key players. Decision-making was confined to a small group; by the time problems surfaced, it was often too late.

Those shortcomings became most pronounced with Buttigieg's plan to knock down or rehab 1,000 vacant homes in as many days that were not being kept up to code. They were concentrated in poor and minority neighborhoods.

Everyone in South Bend agreed that, like so many post-industrial cities, it had a real problem with abandoned homes, but many took issue with the way Buttigieg’s administration tackled it. In particular, leaders and residents of the west-side neighborhoods whose homes were largely targeted said Buttigieg moved too fast and made the data on which the plan was based the priority over the input of the people affected.

"Part of his data-driven approach, and part of the ambitious timelines he set for himself, meant that when things went wrong, they often did harm before things could be fixed," said Nate Levin-Aspenson, a South Bend activist with the local chapter of Indivisible, a national progressive group. "That’s kind of the peril of taking a data-driven approach. Your data and algorithms always reflect the biases of the people who create them."

James Mueller, a high school friend of Buttigieg who served as his chief of staff and is now the Buttigieg-endorsed candidate to succeed him as mayor in the November election, acknowledged there was a steep learning curve.

"Certainly, smart 20-somethings think they know more than they do. And so he wasn't immune to that at different points in his life," Mueller said. "I'd say we've all grown since 29."

A self-described introvert, Buttigieg found that one of the most trying parts of the job was all the standing around and smiling and polite applause he had to take part in.

But when an 18-year-old African-American was shot and killed while sitting on his front porch near Buttigieg's boyhood home on the city's northwest side, the mayor met with the mother of the victim.

"I had no relevant skills for this situation; nothing from my McKinsey training or college education was going to be useful here," Buttigieg wrote in his memoir, "Shortest Way Home: One Mayor's Challenge and Model for America's Future."

"What mattered to her was that I showed up," he continued. "In contrast to my student or consulting days, the value was not in the cleverness of what I had to say, but simply the fact of my being there."

Isaac Hunt, who leads a crime-reduction effort called the Group Violence Intervention that Buttigieg brought to South Bend, called it an "aha moment."

"You could see it in is face," Hunt said, recalling how Buttigieg looked after meeting with the murder victim's mother. "I think it was the first time he'd seen something like that, and it was like, 'Wow, I didn't know.'"

"From that moment on, I've seen him operate differently," Hunt added.

In the months and years to come, Buttigieg would learn to listen carefully to how his decisions affected his constituents and would come to heed their concerns with a revamped approach that relied more on personal experiences and narratives and less on data.

He began trying to communicate better with the city's minority community, making efforts to listen and learn about how his "1,000 Homes" and downtown revitalization initiatives were not helping low-income neighborhoods.

"You have to give credit to an administration that is now becoming responsive," said Regina Williams-Preston, a Common Council member who has been critical of Buttigieg. "There's something to be celebrated about how we've moved through this process and deepened our relationships with one another by kind of allowing ourselves to wrestle with those issues because a lot of times, we don't want to talk about it."

Buttigieg also started meeting quarterly with the city's Latino leaders and began acting on their concerns.

He made sure the new 311 phone line for city services had operators who spoke Spanish, and to help undocumented immigrants oversaw the creation of a "Community Resident Card" program. Financed privately by a local Latino advocacy group and authorized by mayoral executive order, the program provided migrants with a semi-official ID card they could use to pay water bills, get library cards, open bank accounts and pick up prescriptions without fear that their names would eventually be turned over to immigration authorities.

Meanwhile, Buttigieg himself was changing.

In 2014, he deployed to Afghanistan for six months with the Navy Reserve. Stripped of his power and his fancy degrees, which were of little value in a war zone, Buttigieg — who went to a private high school and then Harvard — grew closer to fellow Americans from all walks of life.

"When you're in a really intellectual environment your whole life, you're really smart, you're going to really good schools, there's maybe a little understanding of everyday life that you might not have," said Judith Fox, a Notre Dame Law Clinic professor who has known Buttigieg's parents for years. "But when you're working in the military with other soldiers, the reality of other people's lives hits you in a more concrete way."

And as he considered his own mortality, he realized he had to be true to himself about his sexuality — "to reconcile my professional life with the fact that I am gay," as he wrote in his memoir.

South Bend Mayor Pete Buttigieg hugs his husband, Chasten Glezman, after Buttigieg announced that he was seeking the Democratic presidential nomination.Darron Cummings / AP file

And reconcile he did: He came home in September, announced his re-election campaign in November, came out as gay in a column for The South Bend Tribune the next June, met his future husband, Chasten Glezman, in August, and won re-election with more than 80 percent of the vote three months later. Buttigieg and Glezmen were married last summer.

"He already had the boldness, but when you've got the experience to back up your boldness, then you have complete confidence," said Kathy Schuth, the executive director of the Near Northwest Neighborhood, a nonprofit neighborhood development agency. "In his second term, I think he found his place."

One person who agrees that the mayor grew on the job is the mayor himself.

"They say you get more stubborn with age, but experience taught me to be more open-minded and understanding," Buttigieg said in the interview. "Many times when I felt sure I had the right answer going into a situation, I learned to be a little more humble about it."

The Boykins situation, he recalled of his demotion of the police chief, taught him the significance of a single decision.

"When it hit my desk, I thought of it strictly in terms of making sure I was doing the right thing legally and making sure that the least bad outcome happened. I expected that the more I explained it, the more people would understand — and actually the reverse was true," he said. "You can respond to that (by) being mad or you can respond to that by asking yourself, Why?"

On the 1,000 homes in 1,000 days project that drew much criticism, he says he would have relied more on feedback from the community and less on the plans and analyses.

"I have never over-done listening and community engagement," he admitted. "I thought I was doing a good job, but in retrospect, I know you can't have too much of it."

Buttigieg is a delegator by nature, so some of the bedside-manner aspects of governing didn't come naturally.

His style is in many ways the opposite of that of another presidential contender, Sen. Cory Booker, D-N.J., who as the mayor of Newark was known for personally responding to tweets about unplowed roads by showing up with a snow shovel and digging in.

To Buttigieg, that's an inefficient use of an executive's time.

"Occasionally, it can be a good look to be hands-on, and I do that sometimes," he said, adding that he's gone out to fill potholes with a road crew and offering three crisp reasons why: to express his appreciation for their work; to better understand their job; and to be seen as committed to the issue.

"But it's not like I should actually be a major point of contact on filling potholes," he said. "The thing you have to do in a very senior job is do things that only you can do."

He recalled what the troops said in Afghanistan: "You never want a colonel to get caught up doing lieutenant stuff."

Still, he's come to appreciate that voters want direct contact with their elected officials. Buttigieg started holding "Mayor's Night Out" events where residents can get a few minutes of one-on-one with the mayor and other city officials to air grievances.

Last month, when a gunman killed 50 people at two mosques in New Zealand, Buttigieg got a call from a local Islamic leader asking for an increased police presence at Friday prayers.

"I probably would have handled it differently if it happened in my first year," he said in the interview. "I would have looked after what he asked and thought my job was done."

"But part of what he was asking for, without asking for it, was some kind of reassurance that the Islamic community was safe,” Buttigieg added.

So after hanging up the phone, Buttigieg wrote a letter to the Islamic community, saying, "I want you to know that this entire City has its arms around you, in love and peace, and that we support you as you practice your faith here in this community, our community, this home we share."

He tweeted it, the letter went viral and people from South Bend and around the world cited it as a pitch-perfect example of understanding and leadership.

"I heard a lot more about that letter than I did about the extra police cruiser," Buttigieg said.