The agreement came after months of fraught negotiations.