Moorish rule was soon purely nominal; the Cerdanya was conquered by Charlemagne shortly after the surrender of Girona in 785. The first Count of Cerdanya that we know of by name was Borrell I (798), who was subject to the Count of Toulouse. At this date, Cerdanya was united in government under one count with County of Besalú and Urgell; it was part of the after 817. Cerdanya and Urgell remained united until 897 and both were under the influence and often control of the Count of Aragon. In 842, the Emirate of Córdoba invaded Cerdanya in an attempt to regain the Pyrenees, but they were repulsed by Count Sunifred.