refactor reverse_indices to be callable as an independent function