Script now working, after lots of faffing around.