XML Gallery photo Loop w/out Slideshow

Hi everyone! First question, but I’ve loved this site since it taught me my first line of PHP. Anyhow, here’s my query concerning this tutorial:

http://www.kirupa.com/developer/mx2004/xml_slideshow3.htm

Is there a way to get the photos to loop by clicking on the next button without using the slideshow function?

I want the images to return to the first image when the next arrow is clicked and the last image is being displayed. I tried amending the code as such:

(The bolded section is what I added from the slideshow tutorial to the original photo gallery tutorial.)

function nextImage() {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
if (p == (total-1)) {
p = 0;
firstImage();
} else {
nextImage();
}

}
}
}

But all I end up with is a broken photo gallery that won’t proceed beyond the first image. In effect, returning it to the first image every time the next button is clicked.

Any thoughts?