Fixed index construction
Fixed a few bugs