refactoring index assignment logic