Committing this code even though it doesn't quite work -- I've thought of a better way to do this, and am going to rewrite.