Fix invalid index crash