Thanks for checking the patch!
So what's left to fix?* Moving the escaping-related functions to separate module,* applying your corrections.
Did I missed anything?
I'll submit corrected patch after the weekend.
M