Fixed some logic errors.