 After Boleslaus Yuriy of Masovia and Halych death in 1340, the rights to his domain were passed to his fellow Piast dynast and cousin, king Casimir III of Poland.The local nobles elected one of their own, Dmytro Dedko, as ruler, and repulsed a Polish invasion during the wars over the succession of Galicia-Volhynia Principality when King Casimir III undertook an expedition to conquer Lviv in 1340, burning down the old princely castle.After Dedko's death King Casimir III finally returned and his forces occupied Lviv and the rest of Red Ruthenia in 1349 when Casimir built two new castles.