Thumbnail Rows/Columns Generation through XML Problem

I’ve been looking through some topics and tutorials to see if I could find anything that would improve my grasp on this subject. I’d like to ask for suggestions as well as present my problem.

I am attempting to create an Image Gallery but to be more specific, right now I am trying to get through the generation of thumbnails. The Thumbnail grid will always be 5 Columns by 3 Rows and the amount of Images passed into an array might change. So keeping that in mind, eventually I will create a page system (but not yet, want to get through this first part).

So far this AS code speaks to the MC imageSection and within it to another called thumbMC. I am duplicating this thumb but I can’t seem to get the rows/columns correct. I am confused mainly on the understanding of how I should go about doing this. Suggestions are very welcome. I would really like to know what other approach I can make.

Here is the AS so far (XImages is an array holding the image’s thumburl, imageurl and description) to give you an idea of my approach so far:

function loadViewer() {
	var nSpacing = 94;
	var j:Number = 0;
	var p:Number = 0;
	var nRows = 3;
	var nColumns = 5;
	var nTotal = XImages.length;
	var d = imageSection.getNextHighestDepth();
	for (var i:Number = 0; i<XImages.length+1; i++) {
		imageSection.thumbMC.duplicateMovieClip("thumbMC"+i, d++);
		if (j<nColumns) {
			imageSection["thumbMC"+i]._x = (nSpacing*i)+nSpacing/2;
			j++;
		} else {
			//imageSection["thumbMC"+i]._x = nSpacing/2;
			if (p<nRows) {
				imageSection["thumbMC"+i]._y = (nSpacing*p) + nSpacing*1.4;
				p++;
				trace(j);
				trace(p);
				j = 0;
				trace(j);
			}
		}
		trace("IMAGE ("+i+") X Value ("+_root.imageSection["thumbMC"+i]._x+")");
		trace("IMAGE ("+i+") Y Value ("+_root.imageSection["thumbMC"+i]._y+")");
	}
}