Drawing api

Hi there.

I have this problem with the drawing api and especially with curveTo function. i have these 8 or even more points. Doesn’t really matter, and i want to connect them all with curved lines.
[40,0],[460,0],[500,40],[500,340],[460,380],[40,380],[0,340],[0,40]

Initially it will look like a rounded rectangle. I know about the drawRounded blah blah blah built in function but i need to do it this way. I want to animate each point’s x,y randomly and redraw the curves on tween updates. but i can’t do the curves properly.

This is what i get. I have tried everything, and i just can’t get the curve closed.

This is my function

function updateCurves():void {

 var lineDrawing:MovieClip = new MovieClip();     
 var thisPoint:MovieClip = new MovieClip();
 var nextPoint:MovieClip = new MovieClip();
 
 this.addChild(lineDrawing);
 lineDrawing.graphics.clear();
        
 var startX:int = points[0].x;
 var startY:int = points[0].y;
 var endX:int, endY:int;
 lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
 lineDrawing.graphics.beginFill( 0x990000, 1 );
 lineDrawing.graphics.moveTo(startX,startY);
        
 for (var h:int = 0; h<points.length; h++) {
      thisPoint = points[h];
      nextPoint = points[h+1];
      endX = (thisPoint.x+nextPoint.x)/2;
      endY = (thisPoint.y+nextPoint.y)/2;
      lineDrawing.graphics.curveTo(thisPoint.x,thisPoint.y,endX,endY);
      }

}

Please give feedback on my function…

Many thanks in advance