Fix some logic issues