Kansas Republican Sam Brownback, one of the most unpopular governors in the country, barely survived a vote to confirm his bid for a big Trump administration job on Wednesday, as Vice President Mike Pence broke two ties that kept his nomination alive.

"I'm glad to have the vice president in my corner," Brownback told reporters seconds after Pence cast the tie-breaking vote.

By the slimmest of margins, two dramatic party-line votes in the U.S. Senate on Wednesday cleared the way for Brownback's confirmation to an at-large ambassadorship for international religious freedom, based in Washington, D.C.

The 50-49 votes ended a frustrating six-month wait for Brownback, who was nominated for the ambassadorship by President Donald Trump in July.

The final Senate vote to confirm the nomination took place Wednesday around 5 p.m. EST, officially clearing the way for Lt. Gov. Jeff Colyer, a Johnson County surgeon, to succeed Brownback as governor.

Brownback has faced strong Democratic opposition to his nomination over his record on gay rights, so he needed unified Republican support on Wednesday for the Senate to proceed to the final confirmation vote. Republicans control 51 of the Senate's 100 seats.

With Arizona Republican John McCain absent Wednesday to receive treatment for cancer and Tennessee Republican Bob Corker traveling overseas, Brownback had very little margin for error.

His former Senate colleagues ended up deadlocking 49-49 over whether to let his nomination proceed. Pence, who returned from an overseas trip in the early hours of Wednesday morning, rushed to the U.S. Capitol to break the tie. This was the seventh time Pence has had to cast a tie-breaking vote, with the eighth cast for Brownback's final confirmation vote later Wednesday.

Not a single Democrat crossed the aisle to support Brownback, who once was seen as a rising Republican star and possible presidential candidate. Even senators who had served with Brownback for years in Congress voted no, including Dianne Feinstein of California, Patrick Leahy of Vermont, Jack Reed of Rhode Island, Richard Durbin of Illinois and Senate Minority Leader Chuck Schumer of New York.

During the tense vote on the Senate floor, Schumer could be heard saying to Republican Sen. Jerry Moran of Kansas, "C'mon this guy screwed up your state." Moran crossed his arms and stepped away.

In Topeka, some lawmakers reacted with surprise that the procedural vote had become a nail-biter.

"He had been there with that group for years and you would have thought he would have had more of a broad base unless it's turned over that much in six or eight years," state Sen. John Skubal, a Republican, said.

State Sen. Laura Kelly, a Democrat running for governor, said she wished if it were going to happen that it would have happened sooner "so that we wouldn't have started off this legislative session in such a chaotic manner."

"I think there's a sigh of relief," said state Sen. Julia Lynn, a Republican, said about Brownback. "We're happy for the governor. I think he's very happy to move on to the second or third or fourth phase of his political career."

Brownback appeared at a school choice rally at the Kansas Capitol on Wednesday, potentially one of his last public appearances as governor.

Speaking to children at the rally, Brownback said he had a "little vote" coming up later, adding that he would "see how it goes."

"Normally, these things would have moved forward much more expeditiously, but it hasn't and it's been a difficult time for the United States Senate, the Congress, this year. But I'm glad it's coming up," Brownback told reporters.

Now officially confirmed, Brownback will oversee the country's advocacy for religious minorities in areas of religious conflict and oppression around the globe. The position, which is based in Washington, D.C., was established in 1998.

Brownback's likely return to Washington, where he spent 16 years as a member of the U.S. House and Senate, follows a year when the governor saw his power diminished and his signature tax cuts dismantled by the Kansas legislature. It also would end a period of prolonged awkwardness in the Kansas Capitol in Topeka, where Brownback and Colyer handled key duties.

Brownback points to the tax cuts and other policy moves at the federal level as vindication of his policy experiments.

"We opened up a new area of tax policy, I think, on small businesses_ and they did it. I think you're going to see a new welfare reform along the lines of what Kansas did. That will be a national policy," Brownback said.

"We're not the only state that's done these things. But we're one of the most aggressive ones that have done it."

Senate Democrats had objected to Brownback's selection based partly on his decision to rescind an anti-discrimination protection for LGBT state workers during his fifth year as governor.

Human Rights Campaign, an LGBT advocacy group, opposed his nomination, urging senators to vote against him. Planned Parenthood also urged a no vote, calling Brownback an "extreme ideologue" and criticizing his record on LGBT rights and women's health.

Colyer has promised to listen more to Kansans and to set a different tone, but he has so far resisted discussing specific policies.

Brownback pointed to the abortion restrictions he signed into law during his seven years as his most significant legacy, contending that his policies had lowered the state's abortion rate, a claim that Planned Parenthood disputes.

"17,000 kids alive would be my biggest one," Brownback said of his achievements.

After making racist, anti-Semitic or homophobic comments, elected officials often stay in office, either by apologizing or attacking their opponents. But public servants may have a harder time keeping their jobs.

In a decision that stunned both parties, Judge G. Bryan Collins ruled last Friday that the state's lawmakers don't have the power to pass constitutional amendments. His reasoning traces back to racial gerrymandering.