fixed some logic bugs