Code refactoring in index script