Redo 4fcca80fa4b7d677118a0a2ffedb9d022e8d7234
Redo a0513018b69532c4d2b4f7f39800ff7a541bbca8
re-added some debug output which was lost on rebase