Fix some logical errors