I did something along these lines but instead of the
if instance variable = x create xx
if instance variable = y create yy
etc
I said
required xx
create family
does family = xx then great
else
destroy and re call function
so it loops through until it gets the required type might require several loop might get it first time...
I can dig it out if you want to have a look ?