When Ioannes II arrived back near Kastamon, he found Muhammad ibn Danishmend in charge of the defence (or a female defender). Muhammad was on very bad terms with the sultan Mas'ud of Ikonion, so that an opportunity existed for Ioannes. He sent an embassy and succeeded in persuading Mas'ud into an alliance against Muhammad. Mas'ud sent a nobleman with an army, and he and Ioannes attacked Kastamon together. This put Muhammad in an impossible position. He therefore wrote to Mas'ud, successfully pleading the need for Muslim solidarity. Mas'ud withdrew his allied troops during the night, and the course of the campaign changed against the Byzantines