Bungled family reunifications left 37 migrant children waiting in vans for up to 39 hours last July in Texas, NBC News reports.

The children’s facility was only a 30-minute ride from the adults-only detention center where their parents were being held. Children and parents had been separated when the Trump administration began methodically separating migrant families who illegally crossed into the U.S.

But when the children, who were all between 5 and 12-years-old, arrived at the adults-only facility, they were made to wait in vans so they could be processed and reunified with their parents. When the children arrived, the parking lot was full of other vans like theirs too. Most spent 23 hours in the vehicles; the last child left to be reunited spent 39 hours in a van.

NBC News acquired email correspondences between employees of BCFS Health and Human Services, the government contractor, and the nonprofit organization that were in charge of transporting the children.

“The children were initially taken into the facility, but were then returned to the van as the facility was still working on paperwork,” BCFS regional director Andrew Carter explained in an email sent in the middle of the night. “The children were brought back in later in the evening, but returned to the vans because it was too cold in the facility and they were still not ready to be processed in.”

ICE and U.S. Department of Health and Human Services (HHS) officials tried to resolve the issue. “DHS [the parent agency of ICE] was clearly not ready to deal with the separations and did not take steps necessary to ensure a speedy reunification with their parents,” a former HHS official told NBC News. "Had DHS acted differently, the process would have been much smoother and the impact on the kids would have been much less."

Processing the children was crucial since it guaranteed each child was matched with the right parent. Emails showed that the Trump administration had no way to connect children and parents.

Additional vans were brought in for children to sleep in overnight. The reunification process began on Sunday, July 15, 2018. The last child was reunified with the rightful parent at 5:50 a.m. the following Tuesday.

An ICE spokesman called the incident “unusual,” telling NBC News “[f]ollowing processing delays on July 15-16, which resulted in some children staying overnight in [Port Isabel], DHS took immediate action to resolve the situation and the delays were resolved. These children have all been reunited with their parents and since then, no child has spent more than a few hours waiting to be reunited with their parents.”