fix some logic bugs