Fix logic errors