the check in deleteSegment checks for numSegments > 1 and is later used to index this.segments. So this check effictively means that 0 and 1 are not allowed as index. However, index 1 should be allowed to remove the first segment.
So changing the check to numSegments > 0 or >= 1 would fix this big