Imported code from hg repo