The black Apple iPhone sat forgotten on a public bus that looped its way through the bustling streets of Boulder, Colo., in May.

Its owner, Wesley David Gilreath, was already on the radar of the FBI after allegedly posting a “hunting guide” with information about synagogues, mosques and refugee centers. A week earlier, he had been blocked from buying a gun, but he wasn’t charged with a crime. That changed when two transit workers found his abandoned cellphone while cleaning the bus, unlocked it to try to locate the owner and found themselves staring at reams of disturbing images, authorities say.

Gilreath, 29, was charged in federal court Tuesday with possession of child pornography, but officials say his arrest may have also thwarted a domestic terrorist attack.

“The charge in this case demonstrates that the U.S. attorney’s office and our federal and state law enforcement partners will use every available law enforcement tool not just to prosecute federal crimes, but also to disrupt and prevent potential hate crimes,” Jason Dunn, the U.S. attorney in Colorado, said in a statement to local media outlets on Wednesday

According to federal court records, Gilreath sat for an interview with the FBI on Jan. 24, after agents received a tip that someone had been posting online “hunting guides” targeting Jews, Muslims, refugee centers, Bureau of Land Management offices and Montana National Guard facilities, and linked a document labeled as a “Montana Hunting Guide” to the 29-year-old.

Activists in Oregon and Washington state have warned about such guides, which map the addresses of potential targets for white supremacist attacks. They are also a phenomenon that’s well known to the FBI. Investigators have found that the guides often contain information that can be used to “violently target” people with different ethnicities, religious beliefs and political views, the complaint states.

Court records don’t indicate what transpired at the January interview, and Gilreath was not arrested. Four months later, on May 24, he went to a gun store in downtown Boulder and attempted to buy a firearm, handing over his Colorado driver’s license and filling out the mandatory paperwork from the Bureau of Alcohol, Tobacco, Firearms and Explosives. He was rejected. Afterward, the criminal complaint states, he sent a frustrated text message to his father.

“You’ve permanently ruined my ability to buy a gun in Colo. and other states,” he wrote.

The affidavit doesn’t specify why Gilreath was prevented from buying a gun that day. He had previously been charged with misdemeanor marijuana and drug paraphernalia possession in Indiana, but does not appear to have a criminal record in Colorado or any felony convictions that would bar him from purchasing a firearm, according to court records.

Then, on May 31, a week after the failed gun purchase, Gilreath left his iPhone behind on the bus. It wasn’t protected with a passcode, according to the criminal complaint, and when transit workers unlocked it early the next morning, the first thing they saw was the Photos app, which was filled with child pornography. They called the police, who notified the FBI.

According to court records, Gilreath had stashed away thousands of images and hundreds of videos depicting children who ranged in age from infants to preteens. Investigators also found that he had searched the Internet for violent child pornography and a book titled, “The Pedophile’s Guide to Love and Pleasure.”

In a written order on Tuesday, U.S. Magistrate Judge Scott T. Varholak in Colorado ordered Gilreath to be held without bail, saying that there was “clear and convincing evidence” that the 29-year-old presented a threat to the community.

In addition to the “hunting guide,” the judge wrote, investigators had found that Gilreath also kept lists of local mosques and synagogues and their addresses and collected white supremacist writings and paraphernalia. He had also allegedly searched for a book about gaining children’s trust, and repeatedly failed to show up in court.

“I find that there are no conditions or combination of conditions that I can impose to assure the defendant’s presence and the safety of the community,” Varholak wrote.

Gilreath is represented by a federal public defender. His attorney could not immediately be reached for comment late Wednesday.

An investigation into whether he committed other federal crimes separate from the child pornography charge is ongoing, the U.S. attorney’s statement said.