IK/FK Blend System Creator - Crash To Desktop On Delete Variable After Rotating Object. Help!

I've created a rollout script that can collect bones then create an IK/FK Blend system around the bones and has an undo button to delete all created objects.

The Undo button works fine... until I select all of the FK control objects and rotate them. There fine if I rotate them 1 by 1 but if I rotate all, it forgets there in an array and max gets upset =( It returns this error message:

-- Error occurred in UndoButton.pressed(); filename: C:\Users\Ben Styles\Desktop\IKFKScript\maxscriptIKFK2edited4.ms; position: 15865; line: 427
--  Frame:
>> MAXScript Rollout Handler Exception:
-- Unknown system exception <<

delete FKControlObject << line 427

Then if I delete the rest of the object remaining in the scene, max crashes. It does this 100% of the time.

In the create button block I create all the objects and add them to there own arrays, such as IKBones, FKBones and FKControlObjects. On the Undo Button block I delete those arrays along with the reference I set for the IKGoal and IKControlObject.

Here is a cut down version of the script:
Full script can be downloaded Here:
http://www.megafileupload.com/en/file/501703/IKFKBlendSetupCreator-ms.html

Sorry if it's messy or hard to read.

--IK FK Setup Script 1.0 by Ben Styles
---------------------------------------------------
--Creates an IK FK blend setup around set bones.
--Designed for 3ds max 2014
--Email: [email protected]
rollout IKFKBoneSetupTool "Create Ik / FK Setup"
(
	--Variables
	local SortedBoneCollectionList=#()
	local IKBoneColor = color 255 0 0
	local FKBoneColor = color 0 255 0
	local IKBone=#()
	local FKBone=#()
	local FKControlObject=#()
	local IKGoal
	local IKControlObject
	local IKXformModifier= XForm()
	local FKXformModifier= XForm()
	on Create Pressed do
	(
		IKBone=#()
		FKBone=#()
		FKControlObject=#()
		IKGoal=undefined
		IKControlObject=undefined
		if SortedBoneCollectionList.count<3 then
		(
			messagebox( "Needs more then 2 bones.")
		)
		else
		(
			--Loop Through Bone Collection
			for i=1 to SortedBoneCollectionList.count do
			(
				--Create IK Bones
				append IKBone (copy SortedBoneCollectionList[i])
				if i < 10 then BoneName=SortedBoneCollectionList[i].name as string+"IK0"+i as string
				else BoneName=SortedBoneCollectionList[i].name as string+"IK"+i as string
				IKBone[i].name=BoneName
				IKBone[i].width= IKBoneSize.value
				IKBone[i].height=IKBoneSize.value
				IKBone[i].wirecolor=IKBoneColor
				IKBone[i].ShowLinksOnly = SortedBoneCollectionList[i].showlinksOnly
				IKBone[i].ShowLinks = SortedBoneCollectionList[i].showlinks
				--Parent IK Bones
				if i > 1 do IKBone[i].parent=IKBone[i-1]
				--Create FK Bones
				append FKBone(copy SortedBoneCollectionList[i])
				if i < 10 then BoneName=SortedBoneCollectionList[i].name as string+"FK0"+i as string
				else BoneName=SortedBoneCollectionList[i].name as string+"FK"+i as string
				FKBone[i].name=BoneName
				FKBone[i].width= FKBoneSize.value
				FKBone[i].height=FKBoneSize.value
				FKBone[i].wirecolor=FKBoneColor
				FKBone[i].ShowLinksOnly = SortedBoneCollectionList[i].showlinksOnly
				FKBone[i].ShowLinks = SortedBoneCollectionList[i].showlinks
				--Parent FK Bones
				if i > 1 do FKBone[i].parent=FKBone[i-1]
				--Create FK Control Objects
				FKControlObjectName=SortedBoneCollectionList[i].name as string+"FK_Control_Object"+i as string
				if i != 1 and i!=SortedBoneCollectionList.count then
				(
					append FKControlObject(CreateCircleControlObject FKControlObjectName FKBoneColor FKControlObjectRadius.value FKControlObjectThickness.value EnableRenderInViewportFK.checked)
				)
				else
				(
					append FKControlObject(CreateAxialControlObject FKControlObjectName FKBoneColor FKControlObjectRadius.value FKControlObjectThickness.value EnableRenderInViewportFK.checked)
				)
				FKControlObject[i].transform=SortedBoneCollectionList[i].transform
				--Parent FK Control Objects
				if i > 1 do FKControlObject[i].parent=FKControlObject[i-1]
				if i == SortedBoneCollectionList.count do
				(
					--Create IK Goal
					IKGoal = IKSys.ikChain IKBone[1] IKBone[IKBone.count] "IKHISolver"
 
					--Create IK Control Object 
					IKControlObject = (CreateAxialControlObject "IK Control Object" IKBoneColor IKControlObjectRadius.value IKControlObjectThickness.value EnableRenderInViewportIK.checked)
					IKControlObject.transform=IKBone[IKBone.count] .transform
					IKGoal.parent=IKControlObject
				)
				--Create IK/FK Custom Attribute Spinner on First FK Control Object
				if i==1 do
				(
					addModifier FKControlObject[i] FKXformModifier
					AddIKFKAttribute FKControlObject[i]
				)
				--Setup Orientation Constraints
				OrientationConstrain SortedBoneCollectionList[i] IKBone[i] 50
				OrientationConstrain SortedBoneCollectionList[i] FKBone[i] 50
				OrientationConstrain FKBone[i] FKControlObject[i] 100
				--Wire IK/FK Attribute Spinner to Bone Orientation Weights
				paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] SortedBoneCollectionList[i].rotation.controller[#Orientation_Weight_0] "100-iKfK"
				paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] SortedBoneCollectionList[i].rotation.controller[#Orientation_Weight_1] "iKfK"
				--Wire Bone and Control Object Visibility
				if VisibilityCheckBox.checked==true do
				(
					FKBone[i].visibility = bezier_float()
					IKBone[i].visibility = bezier_float()
					FKControlObject[i].visibility = bezier_float()
					paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] FKBone[i][#Visibility] "iKfK"
					paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] FKControlObject[i][#Visibility] "iKfK"
					paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] IKBone[i][#Visibility] "100-iKfK"
			)
				--Add Xform Modifier to IK/FK Bones
				addModifier IKBone[i] IKXformModifier
				addModifier FKBone[i] FKXformModifier
			)
			--Copy IK/FK Attribute Modifier to FK Control Objects
			AttributeModifier=FKControlObject[1].modifiers [1] -- Instance of IK/FK Attribute
			addModifier IKControlObject IKXformModifier
			addModifier IKControlObject AttributeModifier
			for i=2 to FKControlObject.count do
			(
				addModifier FKControlObject[i] FKXformModifier
				addModifier FKControlObject[i] AttributeModifier
			)
			--OrientationConstrain IK Nub
			OrientationConstrain IKBone[IKBone.count] IKControlObject 100
 
			--Wire Xform Modifiers to IK/FK Attribute
			if ScaleCheckBox.checked == true do
			(
				paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] FKBone[1].modifiers[#XForm].Gizmo.controller[#Scale] "[iKfK/100,iKfK/100,iKfK/100]"
				paramWire.connect FKControlObject[1].modifiers[#Attribute_Holder].IKFKcontrols[#iKfK] IKBone[1].modifiers[#XForm].Gizmo.controller[#Scale] "[(100-iKfK)/100,(100-iKfK)/100,(100-iKfK)/100]"
			)
 
			--Lock Transforms
			setTransformLockFlags IKGoal #all
			setTransformLockFlags IKControlObject #{7..9}
 
		)
	)
	on UndoButton pressed do
	(
		/*
		for i=1 to IKBone.count do delete IKBone[i]
		for i=1 to FKBone.count do delete FKBone[i]
		for i=1 to FKControlObject.count do delete FKControlObject[i]
		if IKGoal != undefined do delete IKGoal
		if IKControlObject !=undefined do (delete IKControlObject)
		*/
		delete IKBone
		delete FKBone
		delete FKControlObject
		delete IKGoal
		delete IKControlObject
		for i=1 to SortedBoneCollectionList.count do
		(
			SortedBoneCollectionList[i].rotation.controller = Euler_XYZ()
		)
	)
)
createDialog IKFKBoneSetupTool width:300 height:400

Array Variables are first declared inside the rollout block at line 9.
Objects are created in the CreateButton Pressed block at line 250.
Objects are deleted in the UndoButton Pressed block at line 404.

If anyone could please explain why and/or fix this I will be very grateful. Thanks for listening!

Maniczombie.