Yep, that is it, my variable were not defined from the offset although I'm a bit confused why they wouldn't be defined. Anyway, I check for variables being defined before printing them to my datafile and that solves the issue.
Thanks to everyone who replied...