Refactor how the smallest index is found, and re-assign the idx of structures