refactored logic on index