Fix incorrect order of checking