Large fixup commit

master and the --restore feature branch had diverged a lot and this
caused some headache. Try to fix this here.