factor out common code and add an index-merge tool