fixed some logic errors