The Senate voted Thursday to reinstate a set of expired FBI tools used to investigate terrorism and espionage that lapsed this spring, adopting modest new privacy protections for Americans swept up in national security cases.

The lopsided vote, 80-16, mostly brings to an end a rare, monthslong debate in Congress over the nation’s surveillance laws that has been shaped by Republican indignation over disclosures of mistakes by the FBI in applications to wiretap Carter Page, a former Trump campaign adviser, during the early stages of the Russia investigation.

Despite reservations, President Donald Trump is expected to sign the bill when it reaches his desk. But first, it will have to go back to the House, where lawmakers drafted a bipartisan compromise package in March and must now sign off on additional changes made by senators to limit the government’s powers under the Foreign Intelligence Surveillance Act, or FISA.

A House leadership aide said that the chamber would not vote on the matter Friday, when lawmakers will briefly return to session to consider coronavirus-related legislation. Democratic leaders were still assessing how to proceed.

Like their House counterparts, civil libertarians on the left and the right in the Senate complained that the bill did not go far enough in protecting the rights of Americans. They had pushed to include a much more stringent standard for the FBI to obtain internet browsing and search histories for national security investigations but fell just one short of the necessary 60-vote threshold.

“I’m pleased this body stood in support of strengthening safeguards in the FISA court process,” Sen. Tom Udall, D-N.M., one of those pressing for the stricter rules, said before the vote Thursday. “However, our failure to protect Americans from the federal government looking over their shoulders while they are on the internet and collecting personal information is unacceptable.”

Still, many of the most strident skeptics of government surveillance ultimately voted in favor of the bill in order to make what they considered modest improvements rather than letting current laws stand. Sen. Mitch McConnell, R-Ky. and the majority leader, who has been a supporter of broad surveillance powers, argued that the final product struck the right balance between safeguarding privacy and granting law enforcement tools to keep the country safe.

“I understand that several of our colleagues believe this compromise bill is not perfect,” he said. “Sadly, imperfection is a fact of life when it comes to compromise legislation.”

The Senate first tried to pass the package in March, but with the coronavirus pandemic limiting its activities and several senators demanding a full debate, its leaders were forced to delay. Three key FBI tools expired for use in new investigations in the meantime, although the operational effect was limited because the tools remained available for new use in preexisting matters.

Thursday’s vote would fully reinstate them. They include allowing the bureau to get a court order to obtain business records related to national security investigations and two rarely used special wiretapping authorities, including the ability to swiftly follow a suspect who frequently changes phone lines in an attempt to evade monitoring.

The Senate also backed the House’s decision to end legal authority for an expensive, dysfunctional and already defunct National Security Agency system that had allowed counterterrorism analysts to swiftly gain access to logs of Americans’ phone calls.

None of the three expired powers were involved in the applications concerning Page. But because they were also part of FISA, their built-in expiration date fueled lawmakers’ efforts to adjust the law in light of the furor over traditional national security wiretaps of the sort used to target Trump’s former adviser.

Lawmakers in both parties were motivated to act based on a damning inspector general report that uncovered numerous errors and omissions by the FBI in FISA applications to wiretap Page. And a follow-up study that looked at the FBI’s preparations to seek 29 other FISA orders in unrelated investigations found significant problems with every one of them, suggesting systemic dysfunction. The FBI and FISA court have since tightened rules and procedures.

Before passing the measure, senators voted to add more layers of outside oversight to certain applications. The House bill had already expanded the universe of cases in which the FISA court should appoint an outsider to critique the government’s arguments, adding applications that present exceptional First Amendment concerns.

The Senate broadened the guidelines still further and beefed up the powers of such outside critics. The proposal, advanced by Sens. Mike Lee, R-Utah, and Patrick J. Leahy, D-Vt., drew wide bipartisan support, passing 77-29 over McConnell’s objections.

Under the amendment, investigations involving domestic political campaigns, religious groups and the news media — or an individual prominent in any such organization — are explicitly listed as the sorts of cases that should usually prompt the appointment of an outsider, unless the judges choose to make an exception. The list would also include requests of the FISA court for “approval of a new program, a new technology or a new use of existing technology.”

One recurring debate about FISA overhauls is whether, when there are court-appointed outsiders in novel or important cases, they should be able to appeal a court ruling in favor of government surveillance powers. The Senate’s measure struck a compromise, empowering the outsider to ask the FISA court judge — or the FISA review court — to consider certifying such cases for higher review.

The amendment would strengthen the right of outside critics to see all relevant classified information. It would also codify a recent FISA court order that wiretap applications must include a government certification that it has shared with the court any information it has that could cast doubt on its suspicions that an investigative target is probably a foreign agent. Justifying that suspicion is the standard for obtaining a warrant from the court.

Proposals for broader changes were rejected along the way, though.

On Wednesday, senators narrowly defeated another proposal, championed by Sens. Ron Wyden, D-Ore., and Steve Daines, R-Mont., that would have raised the bar for the FBI to obtain internet browsing and search histories for national security investigations.

That amendment garnered 59 votes, one shy of the 60-vote supermajority it needed under Senate rules to be attached to the bill.

Senators also handily rejected a more sweeping measure proposed by Sen. Rand Paul, R-Ky. Paul’s proposal would have limited the FISA court system to foreigners on U.S. soil, like diplomats, and transferred counterintelligence and counterterrorism investigations involving Americans and permanent residents to the normal court system.