Reorganize index_cast function