Here is some code I've written. It may be useful.