After The Restoration of Charles II, the Earl of Mar was restored as governor, and the castle was frequently used as a prison, housing several Covenanters. James, Duke of Albany, later King James VII of Scotland and II of England, visited the castle in 1681. During this time, the castle's military role became increasingly important, a powder magazine being built in the castle gardens, and a formal garrison installed from 1685. At the accession of King George I in 1714, John Erskine, 6th Earl of Mar was deprived of the governorship, as well as the post of Scottish Secretary. In response, he raised the standard of James Stuart, the "Old Pretender", in the Jacobite rising of 1715. Government troops, under the Duke of Argyll, quickly moved to occupy the fortress, then advanced to Sheriffmuir to block Mar's way. The Battle of Sheriffmuir was inconclusive, but the rising was effectively over. The Jacobite rising of 1745 saw Charles Edward Stuart lead his army of Highlanders past Stirling on the way to Edinburgh. Following the Jacobites' retreat from England, they returned to Stirling in January 1746. The town soon surrendered, but the castle governor refused to capitulate. Artillery works were set up on Gowan Hill, but were quickly destroyed by the castle's guns. Despite victory at Falkirk, the Jacobites withdrew north on 1 February.