refactoring, centralized validating the index from an array