Motion scripting problem

Hi there,
Hopefully someone can give me a hand with this.
I’m trying to create motion using scripting instead of a guide path or tweening event which should occur like this:

Stage default size (550x400)
Instance of a ball (in a movie)
Ball starts on top left corner of stage then travels to top right, then travels to lower right, then travels to lower left, then travels to top left, then repeats.

Basically the ball should just travel clockwise around the inside border of the stage.

The script I’m trying to setup is based on three frames.

First frame:

_x=30;
_y=30;
xspeed=20;
yspeed=20;
leftedge=0;
rightedge=550;
topedge=0;
bottomedge=400;

Second frame:

_x = _x+xspeed;
if(_x+_width/2>rightedge) {
_x=rightedge-_width/2;
xspeed=-xspeed;
}
if(_x-_width/2<leftedge) {
_x=leftedge+_width/2;
xspeed=-xspeed;
}
_y = _y+yspeed;
if(_y+_height/2>bottomedge) {
_y=bottomedge-_height/2;
yspeed=-yspeed;
}
if(_y-_height/2<topedge) {
_y=topedge+_height/2;
yspeed=-yspeed;
}

Third frame:

gotoAndPlay(2);

So, what I’ve been able to do is to get the ball to bounce around randomly inside the stage, which seems simple enough, but in order to get it to just travel around in a straight line clockwise I modified the script in frame 2 like this.

_x = _x+xspeed;
if(_x+_width/2>rightedge) {
_x=rightedge-_width/2;
_y=_y+yspeed;
}

After this I seem to just run into problems. Each time I try and set the logic for the ball to change direction and head back to the left once it reaches the bottom of the stage it either just stops, or bounces slightly back and forth in the lower right hand corner.

Hopefully someone will be able to help me out with this.
Thanks in advance,

Dez