Fixed invalid index bug