I remember seeing some of this work before in a different thread here.