When rotating, grid doesn't align properly

local function getgridsize(animtrue, rotationtodo) --this is the new stuff, and makes rotation work
	if animtrue == true then
		if block then
			local size = block.Size
			local rotatedsize = Vector3.new(
				math.abs(rotationtodo.XVector.X) * size.X + 
					math.abs(rotationtodo.YVector.X) * size.Y + math.abs(rotationtodo.ZVector.X) * size.Z,

				math.abs(rotationtodo.XVector.Y) * size.X + 
					math.abs(rotationtodo.YVector.Y) * size.Y + math.abs(rotationtodo.ZVector.Y) * size.Z,

				math.abs(rotationtodo.XVector.Z) * size.X + 
					math.abs(rotationtodo.YVector.Z) * size.Y + math.abs(rotationtodo.ZVector.Z) * size.Z
			)
			return rotatedsize
		end
	else
		if block then
			local size = block.Size
			local rotatedsize = Vector3.new(
				math.abs(rotation.XVector.X) * size.X + math.abs(rotation.YVector.X) * size.Y + math.abs(rotation.ZVector.X) * size.Z,
				math.abs(rotation.XVector.Y) * size.X + math.abs(rotation.YVector.Y) * size.Y + math.abs(rotation.ZVector.Y) * size.Z,
				math.abs(rotation.XVector.Z) * size.X + math.abs(rotation.YVector.Z) * size.Y + math.abs(rotation.ZVector.Z) * size.Z
			)
			return rotatedsize
		end
	end
end
local function rotate(key)
	if not block then return end
	local newrotation = rotation
	if key == "Z" then
		newrotation = rotation * CFrame.Angles(0, math.rad(90), 0)
	elseif key == "X" then
		newrotation = rotation * CFrame.Angles(math.rad(90), 0, 0)
	elseif key == "Y" then
		newrotation = rotation * CFrame.Angles(0, 0, math.rad(90))
	end
	local gridSize = getgridsize()
	local pos = block.Position
	local snappedpos = Vector3.new(
		math.floor(pos.X / gridSize.X) * gridSize.X + gridSize.X / 2,
		math.floor(pos.Y / gridSize.Y) * gridSize.Y + gridSize.Y / 2,
		math.floor(pos.Z / gridSize.Z) * gridSize.Z + gridSize.Z / 2
	)
	local gridSizeanim = getgridsize(true, newrotation)
	local posanim = block.Position
	local snappedposanim = Vector3.new(
		math.floor(pos.X / gridSizeanim.X) * gridSizeanim.X + gridSizeanim.X / 2,
		math.floor(pos.Y / gridSizeanim.Y) * gridSizeanim.Y + gridSizeanim.Y / 2,
		math.floor(pos.Z / gridSizeanim.Z) * gridSizeanim.Z + gridSizeanim.Z / 2
	)


	local startcframe = block.CFrame
	local endcframe = CFrame.new(snappedposanim) * newrotation
	local info = TweenInfo.new(0.35, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
	local tween = TweenService:Create(block, info, {CFrame = endcframe})
	tween:Play()
	rotation = newrotation
end

I have this “getgridsize” function and this “Rotate” function. Now, the issue with these two, is that when I do the tween, it goes to a position but then sometimes gets corrected when the tween ends to a different position, like this:

This doesn’t seem to happen with sizes like 4x4x4. SlabY is 4x2x4.

How would I fix this?