Well shut test

The main fix is in the third commit from Andreas; some test code has been squeezed in in front,