XML Photogallery - need help for my tweak

Im Trying to modify the AS for the buttons so as to continuously scroll through the images rather than stop at the last image and visaversa.
In the following modified code I have managed to get the last image to run to the first but When you click the previous button on the first image you have to click it twice before it goes to the last image. Any ideas where i might be going wrong so it only needs one click?

Cheers

Jimmy.


function nextImage() {
	if (p<(total-1)) {
		p++;
		if (loaded == filesize) {
			picture._alpha = 0;
			pictu
```html

re.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
}
} else if (p == (total-1)) {
p = 0;
firstImage();
}
}
function prevImage() {
if (p>0) {
p–;
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
} else if (p<1) {
(p=(total));
}
}