The index handling here could use some comments to give an idea of what it's trying to do