I've been trying to work on a method for splitting one large, rectangular part into a set number of smaller parts (always a perfect square amount for mathematical simplicity) along its smallest axis (so that if the part had an x,y,z size of 1,2,3, the generated parts would maintain their x position while varying their y and z positions, all having a constant size), but for some reason, while my code does generate the parts, it does not properly orient them. I know that it won't work for rotated parts or anything because I'm using plain position and not CFrame (not sure how to implement CFrame into this), but the code should be working for non-rotated parts, but I've no luck with them so far. Here is what I've got, I'd greatly appreciate some help in fixing it.

function splitPart(part, numPieces) --numPieces must be a perfect square
	local smallestAxis = "x"
	local size = part.Size
	local pos = part.Position
	part:Destroy()
	if size.X>=size.Y and size.Z>=size.Y then
		smallestAxis = "y"
	elseif size.X>=size.Z and size.Y>=size.Z then
		smallestAxis = "z"
	end
	if smallestAxis=="x" then
		local maxHeight = size.Y
		local maxWidth = size.Z
		local length = size.X
		local height = maxHeight/math.sqrt(numPieces)
		local width = maxWidth/math.sqrt(numPieces)
		local currentX = pos.X --these are offsets from the "origin" bottom left corner
		local currentY = pos.Y-size.Y/2
		local currentZ = pos.Z-size.Z/2
		for i=1,numPieces do
			local p = Instance.new("Part", workspace)
			p.Anchored = true
			p.Size = Vector3.new(length, height, width)
			p.Position = Vector3.new(currentX, currentY, currentZ)
			currentY = currentY + height
			if currentY == maxHeight then
				currentY = 0
				currentZ = currentZ + width
			end
		end
	elseif smallestAxis=="y" then
		local height = size.Y
		local maxWidth = size.Z
		local maxLength = size.X
		local length = maxLength/math.sqrt(numPieces)
		local width = maxWidth/math.sqrt(numPieces)
		local currentX = pos.X-size.X/2 --these are offsets from the "origin" bottom left corner
		local currentY = pos.Y
		local currentZ = pos.Z-size.Z/2
		for i=1,numPieces do
			local p = Instance.new("Part", workspace)
			p.Anchored = true
			p.Size = Vector3.new(length, height, width)
			p.Position = Vector3.new(currentX, currentY, currentZ)
			currentZ = currentZ + width
			if currentZ == maxWidth then
				currentZ = 0
				currentX = currentX + length
			end
		end
	else
		local maxHeight = size.Y
		local width = size.Z
		local maxLength = size.X
		local length = maxLength/math.sqrt(numPieces)
		local height = maxHeight/math.sqrt(numPieces)
		local currentX = pos.X-size.X/2 --these are offsets from the "origin" bottom left corner
		local currentY = pos.Y-size.Y/2
		local currentZ = pos.Z
		for i=1,numPieces do
			local p = Instance.new("Part", workspace)
			p.Anchored = true
			p.Size = Vector3.new(length, height, width)
			p.Position = Vector3.new(currentX, currentY, currentZ)
			currentX = currentX + length
			if currentX == maxLength then
				currentX = 0
				currentY = currentY + height
			end
		end

	end
end
splitPart(workspace.Part, 4)

Note: If someone can come up with a solution that works on all three axis without keeping one constant but also not making parts too small, that'd be great too.