Fix some logic errors