Added index range check