Reorganize indexing and add unit tests