From ca5eaea4faba4a88f81a415e0f2a17d1a05ef3bd Mon Sep 17 00:00:00 2001
From: Pregmodder <pregmodder@gmail.com>
Date: Sat, 21 Apr 2018 15:41:14 -0400
Subject: [PATCH] organFarmOptions.tw cleanup and fixing

---
 src/pregmod/organFarmOptions.tw | 670 +++++++++++++++++++++++++-------
 1 file changed, 537 insertions(+), 133 deletions(-)

diff --git a/src/pregmod/organFarmOptions.tw b/src/pregmod/organFarmOptions.tw
index efaaafcbff7..2855ac9275f 100644
--- a/src/pregmod/organFarmOptions.tw
+++ b/src/pregmod/organFarmOptions.tw
@@ -278,12 +278,24 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 				<<if $activeSlave.dick > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has a penis.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "penis"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "penis"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.prostate == 0>>
 							<<set $activeSlave.prostate = 1>>
 						<</if>>
@@ -295,24 +307,49 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<<set $activeSlave.chem += 20>>
 						<</if>>
 						<<set $surgeryType = "addDick">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "penis"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "testicles">>
 				<<if $activeSlave.balls > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "testicles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.dick == 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the penis necessary to accept testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "testicles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 					<br>You can forgo standard procedure and implant testicles directly into $possessive abdomen.
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "testicles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.prostate < 1>><<set $activeSlave.prostate = 1>><</if>>
 						<<set $activeSlave.balls = 2>>
 						<<set $activeSlave.ballType = "human">>
@@ -332,13 +369,17 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addTesticles">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "testicles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.prostate == 0>>
 							<<set $activeSlave.prostate = 1>>
 						<</if>>
@@ -361,30 +402,79 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addBalls">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "testicles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "pigTesticles">>
 				<<if $activeSlave.balls > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.dick == 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the penis necessary to accept testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
+					<br>You can forgo standard procedure and implant testicles directly into $possessive abdomen.
+					<<link "Implant" "Surgery Degradation">>
+						<<set $cash -= $surgeryCost>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+						<<if $activeSlave.prostate < 1>><<set $activeSlave.prostate = 1>><</if>>
+						<<set $activeSlave.balls = 2>>
+						<<set $activeSlave.ballType = "pig">>
+						<<set $activeSlave.health -= 20>>
+						<<if $organFarmUpgrade == 2>>
+							<<set $activeSlave.chem += 20>>
+						<</if>>
+						<<if $activeSlave.pubertyXY == 0>>
+							<<if $precociousPuberty == 1>>
+								<<if $activeSlave.physicalAge >= $potencyAge>>
+									<<set $activeSlave.pubertyAgeXY = ($activeSlave.physicalAge+1)>>
+								<</if>>
+							<<else>>
+								<<if $activeSlave.physicalAge >= $potencyAge>>
+									<<set $activeSlave.pubertyXY = 1>>
+								<</if>>
+							<</if>>
+						<</if>>
+						<<set $surgeryType = "addPigBalls">>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.balls = 3>>
 						<<set $activeSlave.scrotum = 3>>
 						<<set $activeSlave.ballType = "pig">>
@@ -404,30 +494,79 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addPigBalls">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "dogTesticles">>
 				<<if $activeSlave.balls > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.dick == 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the penis necessary to accept testicles.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
+					<br>You can forgo standard procedure and implant testicles directly into $possessive abdomen.
+					<<link "Implant" "Surgery Degradation">>
+						<<set $cash -= $surgeryCost>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+						<<if $activeSlave.prostate < 1>><<set $activeSlave.prostate = 1>><</if>>
+						<<set $activeSlave.balls = 2>>
+						<<set $activeSlave.ballType = "dog">>
+						<<set $activeSlave.health -= 20>>
+						<<if $organFarmUpgrade == 2>>
+							<<set $activeSlave.chem += 20>>
+						<</if>>
+						<<if $activeSlave.pubertyXY == 0>>
+							<<if $precociousPuberty == 1>>
+								<<if $activeSlave.physicalAge >= $potencyAge>>
+									<<set $activeSlave.pubertyAgeXY = ($activeSlave.physicalAge+1)>>
+								<</if>>
+							<<else>>
+								<<if $activeSlave.physicalAge >= $potencyAge>>
+									<<set $activeSlave.pubertyXY = 1>>
+								<</if>>
+							<</if>>
+						<</if>>
+						<<set $surgeryType = "addDogBalls">>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					<<link "Implant">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.balls = 2>>
 						<<set $activeSlave.ballType = "dog">>
 						<<set $activeSlave.scrotum = 2>>
@@ -450,43 +589,87 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogTesticles"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "scrotum">>
 				<<if $activeSlave.scrotum > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has a scrotum.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "scrotum"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.balls == 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the balls necessary to accept a scrotum.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "scrotum"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Graft on">>
+					<<link "Graft on" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "scrotum"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.scrotum = $activeSlave.balls>>
 						<<set $activeSlave.health -= 10>>
 						<<if $organFarmUpgrade == 2>>
 							<<set $activeSlave.chem += 10>>
 						<</if>>
 						<<set $surgeryType = "addScrotum">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "scrotum"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "foreskin">>
 				<<if $activeSlave.foreskin > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has a <<if $activeSlave.dick > 0>>foreskin<<else>>clitoral hood<</if>>.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "foreskin"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Graft on">>
+					<<link "Graft on" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "foreskin"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.dick > 0>>
 							<<set $activeSlave.foreskin = $activeSlave.dick>>
 						<<else>>
@@ -497,29 +680,61 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<<set $activeSlave.chem += 10>>
 						<</if>>
 						<<set $surgeryType = "addForeskin">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "foreskin"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "ovaries">>
 				<<if $activeSlave.ovaries > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "ovaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.vagina < 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the vagina necessary to accept ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "ovaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<elseif $activeSlave.mpreg != 0 || $activeSlave.bellyImplant != -1>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave's body cavity is filled with another organ.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "ovaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "ovaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.ovaries = 1>>
 						<<set $activeSlave.eggType = "human">>
 						<<set $activeSlave.preg = 0>>
@@ -539,34 +754,61 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addOvaries">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "ovaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "pigOvaries">>
 				<<if $activeSlave.ovaries > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.vagina < 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the vagina necessary to accept ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.mpreg != 0 || $activeSlave.bellyImplant != -1>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave's body cavity is filled with another organ.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.ovaries = 1>>
 						<<set $activeSlave.eggType = "pig">>
 						<<set $activeSlave.preg = 0>>
@@ -586,34 +828,61 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addPigOvaries">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "pigOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "dogOvaries">>
 				<<if $activeSlave.ovaries > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.vagina < 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks the vagina necessary to accept ovaries.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.mpreg != 0 || $activeSlave.bellyImplant != -1>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave's body cavity is filled with another organ.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.ovaries = 1>>
 						<<set $activeSlave.eggType = "dog">>
 						<<set $activeSlave.preg = 0>>
@@ -633,24 +902,49 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "addDogOvaries">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "dogOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "freshOvaries">>
 				<<if ($activeSlave.mpreg == 0 && $activeSlave.ovaries == 0) || $activeSlave.bellyImplant != -1>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave lacks a viable womb.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "freshOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.physicalAge >= 60>>
 					ERROR: this slave's body is too old to handle pregnancy.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "freshOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "freshOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.ovaryAge >= 47>>
 							<<set $activeSlave.ovaryAge = 45>>
 						<<else>>
@@ -675,42 +969,78 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "freshOvaries">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "freshOvaries"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "prostate">>
 				<<if $activeSlave.prostate != 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave already has a prostate.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "prostate"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "prostate"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.prostate = 1>>
 						<<set $activeSlave.health -= 20>>
 						<<if $organFarmUpgrade == 2>>
 							<<set $activeSlave.chem += 20>>
 						<</if>>
 						<<set $surgeryType = "addProstate">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "prostate"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "eyes">>
 				<<if $activeSlave.eyes > -2 && $activeSlave.origEye != "implant">>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave has working eyes.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "eyes"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<elseif $activeSlave.origEye == "implant">>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Remove ocular implants and implant">>
+					<<link "Remove ocular implants and implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "eyes"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $stockpile.ocularImplant++>>
 						<<set $activeSlave.eyes = 1>>
 						<<set _oldEyes = $genePool.find(function(s) { return s.ID = $activeSlave.ID; })>>
@@ -721,15 +1051,26 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<<set $activeSlave.chem += 20>>
 						<</if>>
 						<<set $surgeryType = "newEyes">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>		
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "eyes"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>	
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "eyes"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.eyes = 1>>
 						<<set $activeSlave.eyeColor = $activeSlave.origEye>>
 						/* no way to salvage original eye color */
@@ -738,26 +1079,39 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<<set $activeSlave.chem += 20>>
 						<</if>>
 						<<set $surgeryType = "unblind">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "eyes"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "mpreg">>
 				<<if ($activeSlave.ovaries != 0) && ($activeSlave.vagina > -1) && ($activeSlave.mpreg != 0)>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					ERROR: this slave has existing reproductive completedOrgans.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					ERROR: this slave has existing reproductive Organs.
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpreg"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpreg"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.mpreg = 1>>
 						<<set $activeSlave.eggType = "human">>
 						<<set $activeSlave.preg = 0>>
@@ -777,26 +1131,39 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "mpreg">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpreg"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "mpregPig">>
 				<<if ($activeSlave.ovaries != 0) && ($activeSlave.vagina > -1) && ($activeSlave.mpreg != 0)>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					ERROR: this slave has existing reproductive completedOrgans.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					ERROR: this slave has existing reproductive Organs.
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregPig"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregPig"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.mpreg = 1>>
 						<<set $activeSlave.eggType = "pig">>
 						<<set $activeSlave.preg = 0>>
@@ -816,26 +1183,39 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "mpregPig">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregPig"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<<elseif $completedOrgans[_i].type == "mpregDog">>
 				<<if ($activeSlave.ovaries != 0) && ($activeSlave.vagina > -1) && ($activeSlave.mpreg != 0)>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave has existing reproductive completedOrgans.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregDog"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>>
-							<<if $activeSlave.ID == $completedOrgans[$i].ID>>
-							<<set $completedOrgans.deleteAt($i)>>
-							<<break>>
-							<</if>>
-						<</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregDog"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<set $activeSlave.mpreg = 1>>
 						<<set $activeSlave.eggType = "dog">>
 						<<set $activeSlave.preg = 0>>
@@ -855,22 +1235,40 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<</if>>
 						<</if>>
 						<<set $surgeryType = "mpregDog">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "mpregDog"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 				
 			<<else>>
 				<<if $activeSlave.voice > 0>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
 					ERROR: this slave is not mute.
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "voicebox"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<<else>>
 					<br>&nbsp;&nbsp;&nbsp;&nbsp;
-					<<link "Implant">>
+					<<link "Implant" "Surgery Degradation">>
 						<<set $cash -= $surgeryCost>>
-						<<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "voicebox"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
 						<<if $activeSlave.ovaries == 1 && $activeSlave.hormoneBalance >= 200>>
 							<<set $activeSlave.voice = 3>>
 						<<elseif ($activeSlave.balls > 0) || ($activeSlave.hormoneBalance < -20)>>
@@ -883,10 +1281,16 @@ The fabricator is ready to grow an organ for $object. Extract tissue to begin gr
 							<<set $activeSlave.chem += 10>>
 						<</if>>
 						<<set $surgeryType = "restoreVoice">>
-						<<goto "Surgery Degradation">>
 					<</link>>
 					|
-					<<link "Discard">><<for $i = 0; $i < $completedOrgans.length; $i++>><<if $activeSlave.ID == $completedOrgans[$i].ID>><<set $completedOrgans.deleteAt($i)>><<break>><</if>><</for>><<goto "Remote Surgery">><</link>>
+					<<link "Discard" "Remote Surgery">>
+						<<set _ofo = $completedOrgans.findIndex(function(s) { return $activeSlave.ID == s.ID && s.type == "voicebox"; })>>
+						<<if _ofo != -1>>
+							<<set $completedOrgans.deleteAt(_ofo)>>
+						<<else>>
+							@@.red;Organ not found for deletion!@@
+						<</if>>
+					<</link>>>
 				<</if>>
 			<</if>>
 		<</if>>
-- 
GitLab