What do you mean by "doesn't work"? I'd compile it but I'm too lazy :
At a glance, the switch statement of chooser could be simplified to four if statements with the "or" operator. Also, the aif(x or y)=aif(x or y)(- or +)1; could be changed to aif(x or y)(- or +)=1;

Sorry I can't help more, but I'm in a hurry.