some old code I wrote.  might help