Handle invalid index and missing key