Aargh. All fixed now. Was another braces problem on another line (the elsif condition) that was only evaluated when the if condition was false. I'm a little confused why the error sometimes appeared before the line above and why the line number was the line number of the if clause and not the line number real culprit, the elsif clause.