Here's how I think it should work: https://gist.github.com/narfbg/9643416