Having rejected peace proposals from its enemies, Ivan the Terrible found himself in a difficult position by 1579, when the Crimean Khanate devastated Muscovian territories and burnt down Moscow (see Russo-Crimean Wars), the drought and epidemics had fatally affected the economy, the policy of oprichnina had thoroughly disrupted the government, while the Grand Principality of Lithuania had united with the Kingdom of Poland and acquired an energetic leader, Stefan Batory, supported by the Ottoman Empire (1576). Batory replied with a series of three offensives against Muscovy, trying to cut the Kingdom of Livonia from Muscovian territories. During his first offensive in 1579 with 22,000 men he retook Polotsk. During the second, in 1580, with a 29,000-strong army he took Velikie Luki, and in 1581 with a 100,000-strong army he started the Siege of Pskov. Frederick II had trouble continuing the fight against Muscovy unlike Sweden and Poland. He came to an agreement with John III of Sweden in 1580 giving him the titles in Livonia. That war would last from 1577 to 1582. Muscovy recognized Polish–Lithuanian control of Ducatus Ultradunensis only in 1582. After Magnus von Lyffland died in 1583, Poland invaded his territories in the Duchy of Courland and Frederick II decided to sell his rights of inheritance. Except for the island of Œsel, Denmark was out of the Baltic by 1585. In 1598 Polish Livonia was divided into: