Redo all the code, it's much smaller and better this time :-)