Rotate ribbontool + plugin doesnt mesh well

it easier to show than tell

when its rotated 90deg the tilting thing just breaks
and when its rotated >90deg its flipped?

code for the tilting

	-- gets the z angle
	local x1, y1, z1 = workspace.P1.CFrame:ToEulerAnglesXYZ()
	local x2, y2, z2 = workspace.P4.CFrame:ToEulerAnglesXYZ()
	-- gets the gradient of the curve
	local m = (z2 - z1) / #rails_table
	
	for i = 1, #rails_table do
		
		rails_table[1].PrimaryPart.BrickColor = BrickColor.new("b")
		
		local rail :Model= rails_table[i]
		local p1, p2 = points_table[i].Position, points_table[i + 1].Position
		
		rail:PivotTo(CFrame.new(0.5 * (p1 + p2), p2) * CFrame.fromAxisAngle(Vector3.new(0, 0, -1), m * i + z1))
		
	end