– Former Labor Secretary Tom Perez narrowly beat U.S. Rep. Keith Ellison on Saturday to lead the Democratic Party in its fight against President Donald Trump, leaving Ellison to return to his job as a Minnesota congressman.

Immediately after finishing second, Ellison accepted Perez’s offer to serve as deputy chairman of the Democratic National Committee. Ellison had planned to quit his Minneapolis-area congressional seat if elected chairman, but said he’ll now stay in the job.

Though visibly disappointed by the loss, Ellison hugged Perez several times and vowed to work with him for party unity.

“If people trust me, then they need to come on and trust Tom Perez as well,” Ellison said, quieting angry shouts from some of his most fervent supporters.

Perez was elected on the second ballot at the meeting of Democratic Party leaders, capturing 235 votes to 200 for Ellison. The threshold for victory was 218 out of the 435 total delegates from all 50 states and several U.S. territories.

Perez, who served in President Barack Obama’s Cabinet, must now try to build a path back to political power for a party still reeling from massive losses in November and shut out of power in Washington and in many states. He said he needs Ellison’s support in confronting Trump and winning elections “from dogcatcher to Senate” across the country.

Newly elected Democratic National Committee Chairman Tom Perez gives a victory speech during the general session of the DNC winter meeting in Atlanta, Saturday, Feb. 25, 2017. Perez picked runner-up Ellison to be deputy chairman.

“We understood that in order to move forward it’s imperative to be united,” Perez said. He said that he and Ellison agreed on the chairman-deputy chairman plan some time ago: “We sat down and talked about this campaign and the number one value was that there was no one in this race who wanted to win at any cost.”

This will be the first time in recent history that the DNC has a deputy chairman position. The party also has three vice chairs; former Minneapolis Mayor R.T. Rybak held that position from 2011 until Saturday, when his term ended.

Ellison’s bid was linked with the presidential campaign of Sen. Bernie Sanders of Vermont, who ultimately lost the nomination to Hillary Clinton but received fervent support from the party’s left flank. Ellison was an early supporter of Sanders, who returned the favor by backing Ellison to lead the DNC. Other prominent party leaders to support Ellison included Senate Minority Leader Charles Schumer of New York, Massachusetts Sen. Elizabeth Warren and a majority of Democratic-backing unions.

Ellison, who rallied behind Clinton after she won the nomination, was asked whether his support for Sanders hurt him with Clinton and Obama supporters among the voting delegates. He called that support “a fact beyond change.”

“Have I ever regretted supporting Bernie? Never, not for one second,” Ellison said.

Praise from Obama

Obama did not formally endorse a DNC candidate, though Perez was seen as the favored choice of the Democratic establishment. Perez was publicly backed by former Vice President Joe Biden. In a statement after Saturday’s vote, Obama praised both men.

“I’m proud of all the candidates who ran, and who make this great party what it is,” Obama said. ”What unites our party is a belief in opportunity — the idea that however you started out, whatever you look like, or whomever you love, America is the place where you can make it if you try.”

Ellison had been first to declare his candidacy for party chairman, just four days after Trump was elected and as Democrats reeled from disappointing showings in races for Congress, governorships and state legislatures.

Trump, who engaged Ellison on Twitter last week, tweeted about the race on Saturday. “Congratulations to Thomas Perez, who has just been named Chairman of the DNC. I could not be happier for him, or for the Republican Party!” the president wrote.

In his four-month campaign, Ellison traveled to more than 30 states and crafted outreach plans to target voters in rural and urban areas, the LGBT community, and apartment dwellers. Ellison has long said that many Republican-held House districts and precincts could be won by Democrats if they do better at turning out voters — particularly in nonpresidential election years.

In his new role, Perez is likely to rely on Ellison for fund­raising help. Ellison brought in more than $1 million in donations since November from donors in all 50 states — more than any other DNC candidate. In the next four years, Perez and Ellison will be expected to raise hundreds of millions of dollars for the party.

Rybak said he was disappointed in the results, but that the DNC is more than just its top leader.

“The head of the DNC is a person and we have a really good one in Tom Perez, but Keith Ellison is a major force now in the Democratic Party, far more than he even was before,” Rybak said. “He’s going to be one of the keys to making sure people who are so upset about Trump get energized, but that it’s not just about Trump.”

Across the convention hall Saturday, delegates wearing “Tom” and “Keith” buttons talked and shared lunches. Brian Wahby, a Perez supporter from Missouri, said it was an emotional day for both sides but that most delegates pledged to walk out of the room as friends.

“When we walk out that door, Donald Trump is the president of the United States,” Wahby said. “It’s not about us, it’s about everybody else, all of the people we represent.”

U.S. Rep. Keith Ellison, D-Minn., is in the running to be the next chair of the Democratic National Committee. (Photo: David Zalubowski, Associated Press)

In a meeting after the election, Ellison and Perez greeted delegates together.

“We have a lot of work ahead, we have a lot of work to do and we’re going to do it together,” Perez said.

Ellison, speaking more quietly, said it’s time to move on.

“He’s our leader, he’s our chair,” Ellison said. “I trust Tom Perez.”