Backport some ideas for fixes from Brunel