fix CS and logic errors