In 1440 the town joined the Prussian Confederation and with the confederation in 1454 it joined Poland. During the Thirteen Years’ War (1454–1466) it was briefly captured by the Teutonic Knights, but in 1456 it returned to Poland. The town was successfully defended against the Teutonic Knights in 1463 and 1466. After the peace treaty signed in Toruń in 1466, the town remained under Polish suzerainty as a fief, as part of the Teutonic Order's state. During the last Polish–Teutonic War, which broke out after the newly chosen Grand Master of the Teutonic Order refused to submit to the Crown of Poland, the town was captured and held by the Poles from 1520 until the dissolution of the Teutonic state in 1525. Afterwards it remained part of the secular Duchy of Prussia, a Polish fief until 1657. In 1526 a Lutheran parish was founded. In 1534 a town school was established.