The Royal Navy gained a foothold in the Mediterranean Sea when Gibraltar was captured by the British in 1704 during the War of Spanish Succession, and formally allocated to Britain in the 1713 Treaty of Utrecht. Though the British had maintained a naval presence in the Mediterranean before, the capture of Gibraltar allowed the British to establish their first naval base there. The British also used Port Mahon, on the island of Menorca, as a naval base. However, British control there was only temporary; Menorca changed hands numerous times, and was permanently ceded to Spain in 1802 under the Treaty of Amiens. In 1800, the British took Malta, which was to be handed over to the Knights of Malta under the Treaty of Amiens. When the Napoleonic Wars resumed in 1803, the British kept Malta for use as a naval base. Following Napoleon's defeat, the British continued their presence in Malta, and turned it into the main base for the Mediterranean Fleet. Between the 1860s and 1900s, the British undertook a number of projects to improve the harbours and dockyard facilities, and Malta's harbours were sufficient to allow the entire fleet to be safely moored there.