Ported recent fixes from Haskell