Refactored indexing code.