Generalize, improve and extend

And actually, you know, make it work.