I wrote this entire document (and the library code) virtually word-for-word in my head when I couldn't sleep last night. It remains to be seen if it does actually work as well as i think it should.