The House voted Thursday to pass a bill reauthorizing an important national counterterrorism tool, after a bipartisan fight over privacy protections in the legislation nearly derailed the legislation.

In a 256-164 vote, lawmakers approved a six-year reauthorization of Section 702 of the Foreign Intelligence Surveillance Act, which allows intelligence officials to spy on communications of noncitizens outside of the United States.

The bill includes moderate reforms authored by both the House Intelligence and Judiciary Committees last year. Proponents say the changes will bolster privacy protections by adding a new requirement for intelligence agencies to obtain search warrants to search communications and new congressional oversight of the kinds of searches made by the government. It also limits the use of the surveillance authority so fewer Americans are caught up in the searches and requires reforms to the way intelligence agencies search the contents of electric communications beyond the sender information.

“This really is a compromise,” said House Intelligence Committee Chairman Devin Nunes, R-Calif.

But the bill was controversial among many House members who were looking for more aggressive reforms. The measure passed only after lawmakers defeated two amendments that would have gutted the bill, including one authored by Rep. Justin Amash, R-Mich., that would have replaced the law with far more stringent requirements.

The Amash amendment failed in a 183-233 vote, but provoked a heated bipartisan debate on the nation’s intelligence program and its impact on American privacy rights. It prompted House Speaker Paul Ryan, R-Wis., to take to the floor to note that the language in question deals with spying on foreign nationals to aid U.S. intelligence efforts, and was not aimed at Americans.

The bill also survived an early morning scare when President Trump tweeted out a reminder that 702 authority was what swept up information on some Trump transition team members, who were later unmasked by the Obama administration. Trump noted that this authority led officials to "abuse the Trump Campaign." But he later tweeted that his administration is taking steps to deal with issues related to unmasking, and asked lawmakers to pass it.

Senate Majority Whip John Cornyn, R-Texas, told the Washington Examiner the Senate will take up the House-passed bill. It could be debated as a standalone measure or included in a short-term spending measure expected on the floor next week.

Sen. Ron Wyden, D-Ore., has authored companion legislation to the Amash amendment.