Mercurial support and additional refactoring