Check array index exists before accessing