fix-hooks: fix correctness, improve robustness (fixes #9, #10 and #12).