When the eviction of a Pennsylvania man with a documented history of mental health issues turned into a deadly shootout, police realized they'd found another gap in a system they'd already spent years working to improve. How had the man, Donald Meyer Jr., passed the state's background check?

State police investigated and found that the record of the man's involuntary mental health commitment — which should have barred him from buying weapons — had been lost in transit between the county and state. In response, the state created an audit system to prevent future errors.

As federal lawmakers seek to improve record reporting to the federal background check system after a Texas massacre highlighted the risks of missing records, Pennsylvania offers a potential roadmap: The state has spent the last six years aggressively overhauling records reporting, hunting down reporting gaps like the one that let Meyer buy a gun and modernizing the technology they use to keep guns out of the wrong hands.

“We can’t afford to make a mistake. We’ve got to find these errors and find these vulnerabilities,” Major Scott Price, director of the Pennsylvania State Police’s Bureau of Records and Identification, told NBC News.

As the U.S. Senate takes up gun legislation in Washington, DC , Dr. Gary Lampert (L), a co-owner of the National Armory gun store, helps Cristiana Verro consider fire arms on April 11, 2013 in Pompano Beach, Florida. Joe Raedle / Getty Images file

Over the last six years, police have upgraded the Pennsylvania Instant Check System’s (PICS) technology and system processes and hunted down gaps in record reporting. Price said that they have received $2 million in federal grants to fund their efforts, but also funded significant reforms themselves.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

“The PICS system is strong — it’s a model for this country. Every state should strive to do as well in record reporting as they do,” said William Rosen, the deputy legal director at Everytown for Gun Safety, a group that advocates stricter gun control.

Background checks for gun sellers are far from simple: An inquiry goes by phone or computer to the FBI's National Instant Criminal Background Check System, which searches itself and two databases, the Interstate Identification Index, where criminal histories are stored, and the National Crime Information Center, which houses warrants and protection orders.

Those three systems house records from countless agencies on the local, state, and federal level. Thirteen states run their own background check systems, too, housing state records and submitting copies to the federal systems, according to FBI. Seven states run background checks for certain kinds of weapons, leaving the rest to the FBI.

Some checks are instant, while others flag potential prohibitive records but require FBI investigators to do more research. It's a lot of moving parts, and it's got to move quickly. If a background check is incomplete after 72 hours, the law says the gun seller can complete the sale without the final results.

Mike Acevedo puts a weapon on display at the National Armory gun store on April 11, 2013 in Pompano Beach, Florida. Joe Raedle / Getty Images file

Rosen was quick to note that when the records are properly entered, the system works well. It’s stopped 3 million gun sales to individuals barred from owning a weapon since its inception in 1998.

“The system is intricate, but the solution is simple: remove any barriers, make sure that your records get into the system,” he told NBC News.

The barriers to getting the right records in the right place vary from state to state, but include privacy concerns over mental health records, the intricacies of firearm and criminal law and technological abilities. The Justice Department has ordered a review of the nation's background check system, the National Instant Criminal Background Check System (NICS), aimed at identifying and resolving records reporting issues at all levels of government, as well as within the military.

Price said the biggest barrier to more accurate record keeping in Pennsylvania is the differences between state and federal laws.

A domestic violence conviction anywhere in the U.S. bars gun ownership, but Pennsylvania doesn't have a specific misdemeanor charge for domestic violence. This makes tracking which misdemeanor offenses meet the federal qualifications of domestic violence — which requires that the assailant and victim live together, have a child together, or are married — difficult. Next year, Price said the state and local police will begin a process, thanks to a $1.3 million federal grant, to collect more information like the relationship of victim and assailant on their fingerprint cards to make accurate reporting much easier.

When it comes to mental health records, Price said the question of how certain involuntary mental health commitments — like the ones done by a concerned family member, instead of a judge — jived with federal law, left them in limbo: Would they be enough to bar a gun sale? The issue was settled in 2013 (all of the state's involuntary mental health commitments were enough to block a gun sale) and the state transferred 643,167 mental health records into the federal system. In the four years since, Price said they’ve added around 200,000 more mental health records to the federal background check system.

According to the National Shooting Sport's Foundation's FixNICS campaign, the state has submitted the most mental health records per capita of any state in the country as of the end of 2016, the most recent data available.

“When we looked at this issue in 2011, Pennsylvania was one of the worst. … They hadn’t submitted a single mental health record,” Rosen said. “By the time we circled back in 2014, they were the best state in the country with hundreds of thousands of records in the system.”