Rati Gelashvili reported a rare but annoying bug in the hash function handling.

The fix requires a significant reworking of the function handling. This
is a first attempt that desperately needs regression testing.