Fixed some logic issues