Where to do clamping on custom camera script?

hi. im trying to make a custom camera script to go along with some other custom stuff im making. ive got the concept down at a basic level but the camera can roll around all willy nilly as seen here:
https://gyazo.com/c1f1bcdbd7434660a71634d0049c507a
obviously, this isnt desired behavior. if you face forward it works correctly but if you start to turn backwards the Y axis randomly inverts. im pretty sure this is an issue with how im treating my values, but im unsure of the mathematical processing i need to do with them in order to get the camera to behave correctly. the default camera scripts are a bit of a miss in terms of readability, so no luck there.

code
function CameraController:Start()
	local plr = self.Player
	
	self.Cam = workspace.CurrentCamera
	self.Enabled = true
	self.SensitivityX = 0.2
	self.SensitivityY = 0.2
	self.CamOffset = Vector3.new(0,3,0)
	
	self.Pitch = 0 --how far we've traveled in total on the Y axis
	self.Yaw = 0 --ditto, X axis
	self.RotateInput = ZERO_VECTOR2 --where we want to go this frame
	
	runService.RenderStepped:Connect(function()
		self:Update()
		
		if not self.Cam then
			return
		end
		
		local chr = plr.Character
		local camCF = self.Cam.CFrame
		
		if chr and chr.PrimaryPart then
			local root = chr.PrimaryPart
			
			self.Cam.CFrame = CFrame.new(root.Position + self.CamOffset) * CFrame.Angles(math.rad(self.Pitch),math.rad(self.Yaw),math.rad(0))
			--self.Yaw = 0
			print(self.Yaw)	
		end
	end)
	
	userInputService.InputChanged:Connect(function(input,processed)
		if not processed then
			if input.UserInputType == Enum.UserInputType.MouseMovement then
				if self.Enabled and self.Cam then
					local delta = input.Delta
					local deltaX = delta.X * self.SensitivityX
					local deltaY = delta.Y * self.SensitivityY
					
					self.Pitch += -deltaY
					self.Yaw += -deltaX
					self.RotateInput += Vector2.new(-deltaX,-deltaY)
					--self.Cam.CFrame *= CFrame.Angles(math.rad(-deltaY),math.rad(-deltaX),math.rad(0))
				end
			end
		end
	end)
end

any help is appreciated!

try changing

self.Cam.CFrame = CFrame.new(root.Position + self.CamOffset) * CFrame.Angles(math.rad(self.Pitch),math.rad(self.Yaw),math.rad(0))

into

self.Cam.CFrame = CFrame.new(root.Position + self.CamOffset) * CFrame.Angles(math.rad(self.Pitch),0,math.rad(0)) * CFrame.Angles(0,math.rad(self.Yaw),0)

hey! thanks for replying. i’m doing something today but i can try it later. could you explain how it’s different? it seems like it would be the same operation.

ok, so i tried it. its exactly the same.

i separated the multiplication like @SquarePapyrus12 suggested EXCEPT i reversed the yaw and pitch

CFrame.new(root.Position + self.CamOffset) * CFrame.Angles(0,math.rad(self.Yaw),0) * CFrame.Angles(math.rad(self.Pitch),0,0)