This wasn't a real problem. Mostly I wasn't understanding parts of the code (not familiar with while loops when I wrote this - enough messing around and now I understand). I did end up fixing the problem with multiple swatches on the same line.