Ok it's all good. I've gotten working now. Apparently the for loop at the end wasn't taking too kindly to my reseting the value of j. I've rewritten it as a while loop and the whole code is working fine now. Thanks again for your help guys.