Fix logical mistakes