Refactor index generation to clean up code