Copied from hg