year later Hubert Walter, Archbishop of Canterbury, persuaded Llywelyn to release him, and Dafydd retired to England where he died in May 1203. Wales was divided into Pura Wallia, the areas ruled by the Welsh princes, and Marchia Wallia, ruled by the Anglo-Norman barons. Since the death of Owain Gwynedd in 1170, Rhys ap Gruffydd had made the southern kingdom of Deheubarth the strongest of the Welsh kingdoms, and had established himself as the leader of Pura Wallia. After Rhys died in 1197, fighting between his sons led to the splitting of Deheubarth between warring factions. Gwenwynwyn, prince of Powys Wenwynwyn, tried to take over as leader of the Welsh princes, and in 1198, raised a great army to besiege Painscastle, which was held by the troops of William de Braose, Lord of Bramber. Llywelyn sent troops to help Gwenwynwyn, but in August Gwenwynwyn's force was attacked by an army led by the Justiciar, Geoffrey Fitz Peter, and heavily defeated. Gwenwynwyn's defeat gave Llywelyn the opportunity to establish himself as the leader of the Welsh. In 1199, he captured the important castle of Mold and was apparently using the title "prince of the whole of North Wales" (). Llywelyn was probably not in fact master of all Gwynedd at this time since it was his cousin Gruffudd ap Cynan who promised homage to King John for Gwynedd in 1199. Reign as Prince of Gwynedd Consolidation 1200–1209 Gruffudd ap Cynan died in 1200 and left Llywelyn the undisputed ruler of Gwynedd. In 1201, he took Eifionydd and Llŷn from Maredudd ap Cynan on a charge of treachery. In July, the same year Llywelyn concluded a treaty with King John of England. This is the earliest surviving written agreement between an English king and a Welsh ruler, and under its terms Llywelyn was to swear fealty and do homage to the king. In return, it confirmed Llywelyn's possession of his conquests and allowed cases relating to lands claimed by Llywelyn to be heard under Welsh law. Llywelyn made his first move beyond the borders of Gwynedd in August 1202 when he raised a force to attack Gwenwynwyn ab Owain of Powys, who was now his main rival in Wales. The clergy intervened to make peace between Llywelyn and Gwenwynwyn and the invasion was called off. Elise ap Madog, lord of Penllyn, had refused to respond to Llywelyn's summons to arms and was