As these emails flew behind the scenes, reports of passengers being detained on arrival were filtering out of airports. Lawyers and protesters arrived, trying to make contact with detainees and demand their release. There was widespread confusion about the impact of the ban, even before court challenges further complicated its implementation. CBP and the Department of Homeland Security did issue some public statements and sets of frequently-asked-questions on their websites that generally track with what is shown in the emails — but lawyers and the public were still scrambling to understand how the ban was being applied.

Late Sunday night, a new instruction went out that the “limited number of travelers subject to the executive orders currently in CBP custody” should be referred for an examination and a potential waiver and that they should not be removed from the country.

An hour later, at 1 a.m., an email clarified that field office directors could give waivers to legal permanent residents themselves, without the State Department weighing in. By 9 p.m. Saturday, another email shows, CBP also had the authority to approve waivers for refugees, unaccompanied children, and certain other people. And that same night, when news came that a federal judge in New York had blocked parts of the order , CBP ordered internally that “we are to suspend all departures of those found inadmissible under the Executive Order, including those who wish to depart.”

And in the middle of the night, a memo arrived instructing CBP officers on how to handle travelers from the countries subject to the ban. The document tells agents to give passengers the option to either withdraw their application for admission to the country or else be put in proceedings for deportation. Some categories of visitor, including permanent residents, refugees, and unaccompanied children, would be held until they could be given an exemption from the order, an authority that initially rested with the secretaries of state and homeland security.

In an update to Homeland Security officials late Friday night, acting CBP commissioner Kevin McAleenan said that about 115 visa holders from banned countries were in the air en route to the United States, along with 61 lawful permanent residents– the largest number of them from Iran.

“Headquarters is currently working on instructions for the field,” wrote CBP’s acting deputy executive director of field operations in an email shortly after the executive order came down. “Please standby for guidance.”

As the agency in charge of admitting people to the country, CBP would have lead responsibility for implementing the president’s new policy. Emails and other government documents, released to The Intercept under the Freedom of Information Act, show how ill-prepared the agency was, crafting guidance on the fly and frantically adjusting its response as thousands of protesters descended on airports around the country.

At the end of the day on Friday, January 27, Donald Trump’s newly signed executive order banning travel to the United States from seven majority-Muslim countries began circulating among officials at U.S. Customs and Border Protection.

“As far as we knew there was no implementing guidance, no indication as to which groups of people this would apply to,” said Betsy Fisher, policy director of the International Refugee Assistance Project, which organized lawyers around the country amid the chaos of the weekend when the ban took effect. “Whether it would include refugees who were cleared to travel, dual nationals, special immigrant visas. On Friday night and Saturday, when we realized our clients were being held, we weren’t aware of any guidance, and the only information we were getting was from the courts. So it was over the next several days that we got the word that legal permanent residents could get in, and that [Defense Secretary] General [Jim] Mattis was intervening to make sure Iraqis could get in, but we were just getting that from the news.” (The White House clarified that the executive order would not apply to Green Card holders on February 1. The Pentagon, reportedly at the request of Mattis, compiled the names of Iraqis who had worked with the U.S. military and who should be exempted from the ban.)

Sirine Shabaya, senior staff attorney with Muslim Advocates, said her group was largely in the dark about who could be issued a waiver. “That was obviously the only way you could get in, but publicly no guidance was released about what documents you should present, who might qualify,” she said.

The documents also offer some insight into CBP’s response to the onslaught of criticism from the public and elected officials. As first reported by the Daily Beast, CBP closely monitored airport protests, noting the number of people and which public figures were in attendance, and instructed employees “NOT to engage with the media or Congressional representatives.” One email alerted field office directors that they were receiving a “high volume of calls from various individuals and others claiming to be attorneys.” The email dismissed the callers as “reading from a script” and said it was “most likely a form of telephonic protest.”

As the Daily Beast noted, CBP also circulated bulletins from various airports, noting the number of protesters, the presence of media and particular political figures, including photos and even summaries of protesters’ chants. (A sample from Washington Dulles airport: “protesters have been witnessed cheering when passengers exit…and occasionally chanting ‘love, not hate. That’s what makes America great.’”) CBP officers were instructed to wear plain clothes and avoid crossing public areas of the terminal. The bulletins also relay information from local police, such as San Francisco police officers saying that “they are hearing ‘chatter’ from the crowd suggesting that ‘they rush the doors.’”

“The purpose of congressmen and lawyers trying to get information about who was getting held and how many people it was, was that there was a real danger that CBP was ignoring the court orders,” said Michael Price, of the Brennan Center for Justice at New York University School of Law.

In an email sent at 8 p.m. on Saturday, acting CBP commissioner Kevin McAleenan noted that the “UN is expressing concern that we are denying G-4s,” a special visa for employees of international organizations and their families, which the State Department had exempted from the effects of the Executive Order.

Canadian border officials were also confused. In an email to McAleenan on Sunday evening, John Ossowski, president of the Canada Border Services Agency, said, “there appears to be an issue with respect to permanent residents and Green Card holders. Our folks are concerned that [CBP] in their written instructions to the field is not clear cut on these issues (apparently it says that individuals could be detained.)” McAleenan wrote back that the guidance was “very clear” that permanent residents “are processed and admitted.” He said that 500 permanent residents had been let in, but that one man in Buffalo “didn’t want to wait for processing and so he went back to Canada. Perhaps that anecdote is lending some confusion.”

Included in the documents are two examples of people who were given waivers and allowed into the United States, though details are heavily redacted for privacy reasons. One was for a child who arrived in Baltimore with their U.S. citizen father, the other an Iraqi refugee at New York’s John F. Kennedy airport who had fled “due to death threats and an attempted kidnapping.” The details visible in the summary of the waiver for the Iraqi refugee line up with those of Haider Sameer Abdulkhaleq Alshawi, one of the plaintiffs in one of the first lawsuits against the muslim ban, in the Eastern District of New York.

A Tuesday email also notes that incoming airline crew members subject to the ban “may be detained on board the vessel…for the duration of the vessel’s U.S. voyage.” In the days after the ban, some flight attendants were reportedly detained.

On the evening of Friday February 3, after a federal judge in Washington state blocked the order, the CBP stopped enforcing the ban, according to an email from the acting commissioner.

As reports from the ground suggested that rank-and-file CBP officers were being overzealous in enforcing the ban and ignoring court decisions, some asked if the U.S. Marshals should be stepping in to enforce them. Emails from the U.S. Marshals Service show bewildered officials inquiring what they were supposed to do about it, after judges in New York and Massachusetts issued orders citing the service. “We’re still wrestling with guidance on this matter,” the chief of public affairs wrote back to a deputy U.S. Marshal in Boston. Another email from an official in Boston says that they “never heard a peep” from the U.S. Attorneys office after they were sent the order. Another email says that the New York judge, Ann Donnelly, had been advised to include the language about the Marshals but “she does not expect us to take any action.”

On the evening of Sunday January 29, CBP head McAleenan wrote an update to DHS officials that they had completed processing of waivers for all the travelers who had been in transit when the ban went into effect. Despite the uproar of the weekend, at least one of the officials thought things were under control.

“Amazing job, sir” wrote Gene Hamilton, senior counsel to DHS. “Y’all did an outstanding job implementing the executive order.”

The documents recently released to The Intercept by CBP are not comprehensive; this reporter has sued the government for failing to process a variety of requests for documents related to the travel ban within the time frame required by law. Lawyers Marcia Hofmann and Kendra Albert of Zeitgeist Law, who are representing the reporter, have asked a federal judge to order the agencies to finish processing the documents by September 5, ahead of when the Supreme Court will hear arguments on the ban in October.

Update, Aug. 8, 2017, 1:42 pm:

This story was updated to include references to additional documents released by the Department of Homeland Security.