Properly handle out-of-range dimension index