added slice bounds check