I was trying to get used to JavaScript scoping rules, and may have applied things inconsistently, will tidy up