Have a look at this. Maybe you can adapt it.

http://www.surefire.com/maxexp/main.pl?p...mp;amp;sesent=0,0