Improved arrayIndex function