However, the King managed to alienate the barons, former supporters, his wife and his son James. He was defeated at the Battle of Sauchieburn and killed in 1488. His successor James IV successfully ended the quasi-independent rule of the Lord of the Isles, bringing the Western Isles under effective Royal control for the first time. In 1503, he married Margaret Tudor, daughter of Henry VII of England, thus laying the foundation for the seventeenth century Union of the Crowns. However, in 1512 the Auld Alliance was renewed and under its terms, when the French were attacked by the English under Henry VIII the next year, James IV invaded England in support. The invasion was stopped decisively at the Battle of Flodden during which the King, many of his nobles, and a large number of ordinary troops were killed. Once again Scotland's government lay in the hands of regents in the name of the infant James V. Government Kingship was the major form of political organisation in the Early Middle Ages, with competing minor kingdoms and fluid relationships of over and under kingdoms. The primary function of these kings was as war leaders, but there were also ritual elements to kingship, evident in ceremonies of coronation. The unification of the Scots and Picts from the tenth century that produced the Kingdom of Alba, retained some of these ritual aspects in the coronation at Scone. While the Scottish monarchy remained a largely itinerant institution, Scone remained one of its most important locations, with Royal castles at Stirling and Perth becoming significant in the later Middle Ages before Edinburgh developed as a capital in the second half of the fifteenth century. The Scottish crown grew in prestige throughout the era and adopted the conventional offices of Western European courts and later elements of their ritual and grandeur. In the early period the kings of the Scots depended on the great lords of the mormaers (later earls) and Toísechs (later thanes), but from the reign of David I sheriffdoms were introduced, which allowed more direct control and gradually limited the power of the major lordships. While knowledge of early systems of law is limited, justice can be seen as developing from the twelfth century onwards with local sheriff, burgh, manorial and ecclesiastical courts and offices of the justicar to oversee administration. The Scots common law began to develop in this period and there were