WASHINGTON (AP) — The gunman who killed 12 people before being shot dead by police at the Washington Navy Yard had a history of violent outbursts, was at least twice accused of firing guns in anger, and was in the early stages of treatment for serious mental problems, according to court records and U.S. law enforcement officials.

But Aaron Alexis, a 34-year-old military contractor and former Navy reservist, apparently managed to exploit seams in the nation's patchwork of complicated gun laws designed to keep weapons out of the hands of dangerous people. He was able to buy a shotgun in Virginia with out-of-state identification, even though that would have prevented him from buying a handgun.

Meanwhile, a picture emerged Tuesday of Alexis as an agitated and erratic man whose behavior and mental state repeatedly came to authorities' attention but apparently never affected his security clearance to do work for the Department of Defense.

Alexis, an information technology employee at a defense-related computer company, used a valid pass Monday to get into the Navy Yard. Officials said besides being armed with the shotgun, he also took a handgun from a law officer. A day after the assault, the motive was still a mystery.

It is illegal for gun dealers to sell handguns to such out-of-state buyers, but the Firearms Owners' Protection Act, passed by Congress in 1986, opened up interstate sales for shotguns and rifles. Virginia gun laws require only that an out-of-state buyer show valid identification, pass a background check and otherwise abide by state laws in order to buy a shotgun in the state. Alexis was never prosecuted for two misdemeanor charges involving guns.

"What the 1986 Firearms Owners' Protection Act did was it made it more convenient for gun buyers," said Kristen Rand, the legislative director at the Violence Policy Center.

Alexis bought the shotgun at Sharpshooters Small Arms Range in Lorton, Virginia, on Saturday, according to a statement from the attorney for the gun range. Michael Slocum said in an email that Alexis rented a rifle, bought bullets and used the range before buying the shotgun and 24 shells. Slocum said Alexis passed a federal background check.

Federal gun laws bar the mentally ill from legally buying guns from licensed dealers. But the law requires that someone be involuntarily committed to a mental health facility or declared mentally ill by a judge, and that information must be reported to the FBI in order to appear on background checks. In the wake of the mass shooting at Virginia Tech in 2007, state authorities changed state laws to make it tougher for the mentally ill to buy guns there.

But Alexis was never declared mentally ill by a judge or committed to a hospital. He had been undergoing mental health treatment from the Department of Veterans Affairs since August but was not stripped of his security clearance, according to two law enforcement officials, who spoke on condition of anonymity because the criminal investigation was still going on.

He suffered serious mental problems, including paranoia and a sleep disorder and had been hearing voices in his head, officials said. But Alexis was not stripped of his security clearance, according to the law enforcement officials, who spoke on condition of anonymity because the criminal investigation was still going on.

He had been suffering from a host of serious mental problems, including paranoia and a sleep disorder, and had been hearing voices in his head, the officials said.

After the December massacre at a Connecticut elementary school, U.S. lawmakers pushed to overhaul gun laws. Among the proposals was a ban on military-style rifles, including the popular AR-15, and high-capacity ammunition magazines. There was also a plan to expand background checks to make sure anyone who wanted a gun got the approval of the federal government.

No legislation has moved forward in Congress, despite urgent pleas from the president, some lawmakers and victims' families.

President Barack Obama has made a few narrow administrative changes, but those are not likely to impact the kinds of guns most often found at crime scenes.

A month before Monday's rampage, Alexis complained to police in Rhode Island that people were talking to him through the walls and ceilings of his hotel rooms and sending microwave vibrations into his body to deprive him of sleep.

Alexis told police he got into an argument with someone as he was getting on a flight from Virginia to Rhode Island, where he was working as a naval contractor, and he said the person sent three people to follow and harass him.

He said he heard voices talking to him through a wall while at one hotel, so he changed hotels twice, but the voices followed him, according to the Newport, Rhode Island police report. He said he feared they might harm him. He also "stated that the individuals are using 'some sort of microwave machine' to send vibrations through the ceiling, penetrating his body so he cannot fall asleep."

Later that day, Newport police alerted the Rhode Island naval station and sent a copy of the police report, Lt. William Fitzgerald said Tuesday.

A spokeswoman for the station had no comment Tuesday.

Alexis came to the Washington area about two weeks after the Rhode Island incident and had been staying at hotels.

The assault is raising more questions about the adequacy of the background checks done on contract employees who hold security clearances — an issue that came up recently with National Security Agency leaker Edward Snowden.

Navy Secretary Ray Mabus ordered two security reviews Tuesday of how well the Navy protects its bases and how accurately it screens its workers.

Similarly, President Barack Obama has ordered the White House budget office to examine security standards for government contractors and employees across federal agencies.

Mert Miller, associate director of Federal Investigative Services for the Office of Personnel Management, said in a statement that federal budget, personnel and intelligence officials were working to "review the oversight, nature and implementation of security and suitability standards for federal employees and contractors."

In general, he said, background security clearance investigations cover information about an individual's criminal history.

Alexis had run-ins with the law in 2004 and 2010 in Texas and Seattle after he was accused of firing a gun in anger.

His bouts of insubordination, disorderly conduct and being absent from work without authorization prompted the Navy to grant him an early — but honorable — discharge in 2011 after nearly four years as a full-time reservist, authorities said.

"He wasn't a stellar sailor," Navy spokesman Rear Adm. John Kirby told CNN. "We know that."

But he said the offenses weren't "grievously serious" and the punishments for them are fairly mild. Kirby said there was a proposal to "administratively separate him from the Navy," with less than an honorable discharge, but Alexis volunteered to leave early and received an honorable discharge.

Kirby said that none of Alexis' offenses while he was in uniform "give you an indication that he was capable of this sort of brutal, vicious violence."

As for Alexis' security clearance, Kirby said he received one around the time he enlisted. "It was good for 10 years. And it was at the secret level. So the security clearance was valid when he left the Navy in 2011. And because he wasn't out of work very long before he took this next job, the security clearance went with him."

Alexis joined the Florida-based IT consulting firm The Experts in September 2012, leaving a few months later to return to school. He came back in June to do part-time work at the Washington Navy Yard as a subcontractor, helping the military update computer systems.

The Experts' CEO, Thomas Hoshko, told The Associated Press that Alexis had "no personal issues," and he confirmed that Alexis had been granted a "secret" clearance by the Defense Security Service five years ago.

Alexis' background check "came back clear," Hoshko said.

Among those killed in the Navy Yard shootings was marine engineer and naval architect Vishnu Pandit, 61, an immigrant from India.

___

Associated Press writers Eric Tucker, Jack Gillum, Lolita C. Baldor,Pauline Jelinek, Matt Apuzzo, Adam Goldman, Jesse Holland, Stacy A. Anderson, Brian Witte and Ben Nuckols in Washington contributed to this report.