Refactoring of index selection code