added proposal from mercurial (with a few fixes)