Added logic to create index for union find