Fixed some logic errors