I knew we had some routines that almost did this, and Dave Hart pointed me at util/tg.c and util/tg2.c - you might want to check those out if you haven't done so already. H