In the United States, anybody who wants to buy a gun from a federally licensed firearms dealer (FFL) is subject to a background check. Since 1998, when the National Instant Criminal Background Check System, or NICS, went online, the Federal Bureau of Investigation has processed more than 320 million of them.

The overwhelming majority of gun background checks take just minutes to clear the would-be buyer. Only 2 percent result in a rejection because of a disqualifying record in the shopper’s personal history.

And then there are the people who slip through the cracks and obtain guns they should have been barred from possessing — sometimes with deadly consequences. The perpetrators of February’s workplace shooting in Aurora, Illinois; the church massacres in Sutherland Springs, Texas, and Charleston, South Carolina; and the 2007 rampage at Virginia Tech each had a history that banned them from owning firearms. Yet none were stopped, because of omissions and loopholes in the system.

It’s such cases that expose the hidden complexities of NICS, and the importance of each element functioning the way that it should.

Given the central role that background checks play in balancing individual Americans’ gun rights and our shared public safety, it’s worth investing a few minutes to understand how they work.

I walk into a licensed gun store and ask to buy a gun. What happens next?

You’ll have to complete Form 4473, which includes 16 questions relating to your background, drug use, and criminal history. The gun store will then contact NICS online or by phone and supply your answers, and your Social Security number.

What does the FBI look for in a background check?

Anything that makes a person too high-risk to possess lethal firepower, in the eyes of the law. Those include criminal and mental health history, dishonorable military discharges, unlawful immigration status, an open warrant, a documented history of domestic violence, and drug use.

Felony convictions are the most common reason for the gun background check system to reject an applicant, resulting in 865,910 denials during the 21 years that NICS has been in operation. More than 189,000 fugitives, 212,000 domestic offenders, and 148,000 unlawful drug users have also been blocked. The bar for denying someone on mental health grounds is very high, requiring that a person has been declared unsound or involuntarily confined to a psychiatric institution by a court or other authority. Fewer than 43,000 people have been denied under this criterion.

Where does the gun background check system get all this information?

To ascertain whether an applicant should be disqualified from owning a gun, the FBI draws from three databases. The Interstate Identification Index is a giant repository of criminal convictions used by an array of law enforcement agencies for a range of purposes. The National Crime Information Center, or NCIC, is an “electronic clearinghouse” of other criminal justice records, including protective orders and disposition records. Finally, there’s the NICS Indices, which include records contributed by federal, state and local agencies specifically flagging individuals prohibited from buying a gun.

The NICS Indices hold records that wouldn’t necessarily show up in the other two solely criminal databases. That includes those related to mental health and immigration status. However, some states like Connecticut add nearly all prohibiting records to the NICS Indices because they typically result in a quick denial, without any need for further investigation by the FBI.

So if someone has done something that leaves them banned from firearm ownership, that record will be in one of the databases, the FBI will see it, and they won’t get the gun?

That’s the idea — but as experts will tell you, the gun background check system is only as strong as the records it contains. Some records never make it into the databases, and others are entered in a way that doesn’t make it crystal clear that the person is barred from possessing guns.

States voluntarily supply records to the databases that make up the NICS system, and they do a spotty job of it. According to a Bureau of Justice Statistics report, at the end of 2014 there were 7.8 million active-warrant records in state warrant databases, but only about 2.1 million such records in the NCIC database.

Mental health records present a particular problem. The only way a history of involuntary psychiatric confinement or a judge’s ruling of mental incompetence will show up on a gun background check is if a state makes the effort to submit it to the NICS Indices — those records aren’t meant for the other two databases. The Virginia Tech massacre, committed by a gunman whose history of severe psychiatric illness had not been forwarded to the the FBI, exposed shortfalls in mental health reporting to the NICS Indices. Several states have since made progress in correcting those omissions, but others have remained resistant, citing privacy laws.

The military has also fallen short in forwarding records from its internal justice system. The names of service members expelled under a dishonorable discharge are forwarded to the NICS Indices, since a dishonorable discharge, on its own, triggers a gun ban. Service members convicted in court-martials for other offenses, like felony assault, domestic violence, or drug possession, may also be prohibited from possessing firearms. Those records go into NCIC. Base officers are supposed to add labels to the paperwork database making it clear when the offender is barred from guns.

In the case of the Sutherland Springs gunman, records from his Air Force court-martial for choking his then-wife and fracturing the skull of his baby stepson were never entered into the FBI’s system. He was able to buy at least three guns from licensed sellers, passing a background check each time. In May, a federal judge ruled that survivors of the massacre and family members of the victims can sue the federal government for negligence.

Do gun background checks work the same in every state?

Yes and no.

Federal law establishes the grounds for gun bans, so generally speaking, a history that gets a person barred in one state will also bar him across the country. But states can also decide that other crimes should also initiate a ban. The federal gun prohibition for domestic abusers doesn’t apply to dating partners, for instance, so 23 states and the District of Columbia have passed their own laws closing that so-called boyfriend loophole.

States also have the option of running their own gun background checks, supplementing the FBI’s databases by sweeping state and local records repositories as well. Twenty one states choose to do so.

Back to my check: How long does it take?

NICS has “instant” in its name for a reason. Department of Justice guidelines require NICS reviewers to make an immediate decision in 90 percent of cases.

If the check comes back clean, the FBI gives the sale a green light. If it doesn’t, the purchase is denied. Sometimes the FBI seeks more information in order to make a final determination. In those cases, the check is transferred to the FBI’s Criminal Justice Information Services Division, where an examiner reaches out to local law enforcement and other state agencies.

So when a gun background check isn’t instant, how long can it take?

When a check requires more information, the FBI has three business days to make a final determination on the buyer. If a decision can’t be made in that time, a licensed dealer is allowed by law to go ahead and sell the firearm, in a sale referred to by the FBI as a “default proceed.”

The dealer is not required to notify the FBI when a sale goes through this way. The NICS examiner tasked with the case is supposed to keep working on it and has up to 90 days to reach a final conclusion.

Default proceed sales can have public safety implications. In April 2015, the Charleston church gunman legally purchased a Glock in such a transaction after a cascade of clerical errors delayed his background check. He was disqualified from gun ownership because of a drug charge. Two months later, he used the weapon he bought to murder nine parishioners at a church in Charleston, South Carolina.

FBI data also indicates that some categories of prohibited purchasers tend to stretch past the three-business day window more often than others. According to a December 2016 Government Accountability Office report, the FBI takes a week to deny sales because of a misdemeanor domestic violence conviction. That’s more time than any other kind of denial, which is troubling considering the strong link between domestic violence, homicide and mass shootings.

Say my background check ultimately does show I’m not allowed to own guns, but after the three-day period has expired? Then what happens to the gun I just bought?

If the FBI determines that the buyer was prohibited, the agency sends out a retrieval order to the Bureau of Alcohol, Tobacco, Firearms and Explosives. The ATF is then responsible for getting the gun back.

Retrieval orders are relatively rare: A NICS operations report from 2000 noted that of more than 45,000 default proceeds issued that year, approximately 5,000 resulted in a retrieval order.