I'm having an issue with rotation a model in my script

function rotateMod(Mod,center,rotation)
	local parts ={}
	local function scan(parent)
		for _,obj in pairs(parent:GetChildren()) do
			if (obj:IsA("BasePart")) then
				table.insert(parts,obj)
			end
			scan(obj)
		end
	end
	scan(Mod)
	for _,part in pairs(parts) do
		part.CFrame = (center*rotation*(center:inverse()*part.CFrame))	
	end
end

Mouse = game.Players.LocalPlayer:GetMouse()
Mouse.KeyDown:connect(function(key)
	if selected == true and key == "r" then
		Rot = Rot + 90
		if Rotation == Vector3.new(0,0,0) then Rotation = Vector3.new(0,90,0)
		rotateMod(p,p:GetModelCFrame(),CFrame.new(0,90,0))
		elseif Rotation == Vector3.new(0,90,0) then Rotation = Vector3.new(-180,0,-180)
		rotateMod(p,p:GetModelCFrame(),CFrame.new(-180,0,-180))
		elseif Rotation == Vector3.new(-180,0,-180) then Rotation = Vector3.new(0,-90,0)
		rotateMod(p,p:GetModelCFrame(),CFrame.new(0,-90,0))
		elseif Rotation == Vector3.new(0,-90,0) then Rotation = Vector3.new(0,0,0)
		rotateMod(p,p:GetModelCFrame(),CFrame.new(0,0,0)) end
		s.Rotation = Rotation
	end
end)

I've tried everything I can think of and the rotations never turn correctly. I think its a numerical flaw that I can't figure out. Any help is appreciated.