The U.S. House of Representatives has voted to rein in the National Security Agency's bulk collection of the country's telephone records, while allowing the agency to engage in more targeted surveillance.

The House voted 338-88 late Wednesday to approve the USA Freedom Act, a bill intended to end the NSA's mass collection of telephone metadata within the U.S. But the bill would extend an expiring provision in the anti-terrorism Patriot Act that allows the NSA to collect U.S. telephone and business records, but with a more limited scope.

While some lawmakers and civil rights groups criticized the bill as too weak, supporters defended it, saying it is a huge step toward restoring privacy protections in the Fourth Amendment to the U.S. Constitution, which protects residents against unreasonable searches and seizures.

The bill gives lawmakers a "rare opportunity to restore a measure of restraint to surveillance programs that have simply gone too far," said Representative John Conyers Jr., a Michigan Democrat and co-sponsor of the USA Freedom Act. "For years, the government has read ... the Patriot Act to mean that it may collect all domestic telephone records, merely because some of them may be relevant sometime in the future."

While more surveillance reforms are needed, the USA Freedom Act is a good first step, said Representative Jim Sensenbrenner, a Wisconsin Republican and primary sponsor of the bill. The bill is the result of nearly two years of negotiations in the House, he noted.

House Republican leaders had threatened to kill the bill if more limits on surveillance were added to it.

While some lawmakers have pushed for reauthorization of Section 215 without any new limits, that would be "irresponsible" following revelations of the NSA phone records program in 2013, said Sensenbrenner, primary author of the Patriot Act, passed in 2001. "Congress never intended Section 215 to allow bulk collection," he said. "That program is illegal and based on a blatant misinterpretation of the law."

The NSA program collects information about telephone calls made in the U.S., including the recipients and the time and length of the calls, but does not record the conversations.

The USA Freedom Act, which now heads to the Senate, would require the NSA to use specific selection terms to target U.S. telephone records. It prohibits the agency from collecting all telephone or business records from a city or zip code. It also would end bulk collection in the U.S. under other federal programs, including the FBI's national security letter program.

The bill would also create a new panel of experts at the U.S. Foreign Intelligence Surveillance Court to advise judges about privacy and civil liberties, communications technology, and other technical or legal matters.

Critics said any extension of the business and telephone records provision, Section 215 of the Patriot Act, would violate the privacy of U.S. residents.

Conservative advocacy group Americans for Limited Government had called on the House to vote against the bill. The bill voted on Wednesday is an "emasculated version" of legislation to end NSA surveillance, Rick Manning, the group's president, said by email.

Section 215 of the Patriot Act expires in June, but the USA Freedom Act would extend the business records collection provision until December 2019.

In April, Senate Majority Leader Mitch McConnell, a Kentucky Republican, introduced a bill that would extend Section 215 without any new limits on the NSA.

The House vote on the bill comes just days after a U.S. appeals court ruled that the telephone records program was not authorized by Congress in the Patriot Act.

A handful of lawsuits have challenged the program and are in various stages in courts across the U.S.