moved index assertion before array access