Rewrite share control, fix #448, fix tests
Merge