refactored index code