SEATTLE (AP) — Recent mass shootings have spurred Congress to try to improve the nation’s gun background check system that has failed on numerous occasions to keep weapons out of the hands of dangerous people.

The problem with the legislation, experts say, is that it only works if federal agencies, the military, states, courts and local law enforcement do a better job of sharing information with the background check system — and they have a poor track record in doing so. Some of the nation’s most horrific mass shootings have revealed major holes in the database reporting system, including massacres at Virginia Tech in 2007 and at a Texas church last year.

Despite the failures, many states still aren’t meeting key benchmarks with their background check reporting that enable them to receive federal grants similar to what’s being proposed in the current legislation.

“It’s a completely haphazard system — sometimes it works; sometimes it doesn’t,” said Georgetown University law professor Larry Gostin. “When you’re talking about school children’s lives, rolling the dice isn’t good enough.”

In theory, the FBI’s background check database, tapped by gun dealers during a sale, should have a definitive list of people who are prohibited from having guns — people who have been convicted of crimes, committed to mental institutions, received dishonorable discharges or are addicted to drugs.

But in practice, the database is incomplete.

It’s up to local police, sheriff’s offices, the military, federal and state courts, Indian tribes and in some places, hospitals and treatment providers, to send criminal or mental health records to the National Instant Criminal Background Check System, or NICS, but some don’t always do so, or they may not send them in a timely fashion.

Some agencies don’t know what to send; states often lack funds needed to ensure someone handles the data; no system of audits exists to find out who’s not reporting; and some states lack the political will to set up a functioning and efficient reporting process, experts said.

“The system is riddled with opportunities for human error,” said Kristin Brown, co-president of the Brady Campaign to Prevent Gun Violence.

A proposal in Congress seeks to establish a structured system for federal agencies to send records to the NICS database. Sen. John Cornyn of Texas says the legislation — often referred to as “Fix NICS” — will save lives.

“We should start with what’s achievable and what will actually save lives, and that describes the ‘Fix NICS’ bill. It will help prevent dangerous individuals with criminal convictions and a history of mental illness from buying firearms,” the Republican said.

Often left out of the debate in Washington is the fact that similar legislation passed after the 2007 Virginia Tech massacre, but many records are still not being sent to the database.

The Justice Department even set up a new grant program that offered states help with their reporting system, but many didn’t even bother to apply. In 2016, only 19 states and one tribe received funds totaling $15 million. The number of states currently participating is 31.

Several states aren’t eligible for the grant because they haven’t set up a system that allows a person who was prohibited from having a gun due to mental health issues get their rights restored. The National Rifle Association has long-pushed for those types of restoration requirements, Brown said.

Important mental health records that would have kept Seung-Hui Cho from getting the guns he used to kill 32 people at Virginia Tech were never entered into NICS. The gunman who killed dozens at a Texas church in November was able to purchase weapons because the Air Force didn’t send his domestic violence conviction to the database.

And the father of a teenager who killed himself and four classmates at a Washington state high school in 2014 was able to purchase several guns, including the one his son used, because the Tulalip Tribal Court had not shared his domestic violence protection order with Marysville, Washington, authorities, who would have sent it to the background check system.

Since then, the tribe received a $333,841 grant to help improve its criminal records reporting.

The man who walked into a Carson City, Nevada, IHOP restaurant with an assault weapon in 2011 and killed four people had a history of mental illness, had been diagnosed with schizophrenia and had been taken into custody by police in nearby California under the state’s involuntary commitment law. But under federal law, people are only prohibited from having a firearm if they have been “adjudicated as a mental defective” or committed to a mental institution.

The federal law doesn’t include involuntary commitments.

Some states have enacted their own laws that limit gun ownership based on mental health issues, but they’re all different, according to a report by the National Conference of State Legislatures.

Experts say many agencies don’t know what type of mental health information to send to NICS. Brown said many agencies, hospitals and treatment providers are under the mistaken impression that federal medical privacy laws prevent them from sharing information with the system.

Carson City Sheriff Ken Furlong, who handled the IHOP mass shooting, said the biggest roadblock to creating a comprehensive NICS system is privacy concerns. Some are afraid that if they report their family members, they’ll be arrested, he said, and agencies feel stifled by privacy laws.

“When someone is in crisis, why are we waiting to respond?” Furlong asked. “We have a public safety responsibility to prevent something from happening before we have to use force.”

The federal legislation being considered in Congress might help ensure more criminal records reach the background check database, but it has limitations because Congress can’t force states to enact laws. And it doesn’t address gaps in mental health commitment reporting, said Gostin.

“Because mental health records are critical to the integrity of the system,” he said, “the bill leave a significant gap.”

Some states have passed their own laws requiring agencies to report to NICS, but few keep track of whether that’s happening and most don’t have penalties for failing to submit records.

“They’re not reporting but there are no repercussions,” said Cassandra Crifasi, with the Johns Hopkins Center for Gun Policy and Research. “What is there to encourage people to follow the law?”