moved writing the index up for efficiency.