Refactoring: simplify build_index