The U.S. has reached an agreement with the Afghanistan government to transfer the Parwan Detention Facility to Afghan control, the Pentagon said Saturday, two weeks after negotiations broke down over whether the U.S. would have the power to block the release of some detainees.

According to a senior U.S. official, a key element to the agreement is that the Afghans can invoke a procedure that ensures prisoners considered dangerous would not be released from the detention center. The agreement also includes a provision that allows the two sides to work together to resolve any differences. The official lacked authorization to discuss the details of the agreement publicly and spoke on condition of anonymity.

Transfer of the Parwan detention center on Monday is critical to the ongoing effort to gradually shift control of the country’s security to the Afghans as the U.S. and allies move toward the full withdrawal of combat troops by the end of 2014.

Afghans demanded control of the center, but U.S. officials have worried that the most threatening detainees would be freed once the U.S. transferred control. Defense Secretary Chuck Hagel spoke with Afghan President Hamid Karzai Saturday as officials finalized the agreement after days of intense negotiations.

The senior official said U.S. and Afghan officials who are familiar with the detainees would meet to assess the potential danger of their release to coalition forces. The official said that more senior level officials could be brought in if there are disagreements but that to date the two sides have been able to agree without bringing in those higher authorities.

Disagreements over the detention facility, which also included whether Afghans can be held without trial, had thrown a pall over the ongoing negotiations for a bilateral security agreement that would govern the presence of U.S. forces in Afghanistan after 2014.

There is an Afghan administrator of the Parwan prison, but the Americans have power to veto the release of detainees. The prisoners held under American authority do not have the right to a trial because the U.S. considers them part of an ongoing conflict.

Pentagon press secretary George Little said Hagel “welcomed President Karzai’s commitment that the transfer will be carried out in a way that ensures the safety of the Afghan people and coalition forces by keeping dangerous individuals detained in a secure and humane manner in accordance with Afghan law.”

Last weekend Hagel spoke with Karzai, and officials said the two men agreed to resolve the thorny issue within a week.

The top U.S. commander in Afghanistan, Marine Gen. Joseph Dunford, has also been working to resolve the matter — one of several divisive issues that soured relations between the U.S., its allies and the Afghans in recent weeks.

The U.S. had been scheduled to hold a ceremony marking the transfer of control two weeks ago, during Hagel’s first visit to Afghanistan as defense secretary. That ceremony was called off after negotiations broke down.

In addition to disputes over the Parwan facility, the U.S.-led coalition and Afghans have wrangled over several other difficult issues. Last month, Karzai insisted that the coalition forces cease all airstrikes, after a NATO assault caused civilian casualties.

More recently, Karzai demanded that U.S. special operations forces leave Wardak province after allegations that U.S. commandos and their Afghan partners abused local citizens. Dunford has denied the charges.

Last week, the two sides reached an agreement on the Wardak issue. Dunford agreed to remove a team of commandos from Wardak’s Nirkh district and transition security of that area to the Afghans as soon as possible.

U.S. special operations forces would remain in other parts of the restive province while the coalition continues to work to transition those areas also to the Afghans.

U.S. officials have made no final decision on how many troops might remain in Afghanistan after 2014, although they have said as many as many as 12,000 U.S. and coalition forces could remain.

There are 66,000 U.S. troops in Afghanistan, down from a 2010 peak of 100,000.