I had to think about what was going on here for a while... it's really smart. I wonder if there aren't simpler ways to do this though (maybe that sacrifice being able to index on arbitrary things?) or if maybe this code lives in a nice abstraction that we could use?