Proper use of 'var' to avoid running conditions

Missing "var" leads to creation of variables in global scope which should only be in local scope. With multiple users and Retain activated, this can lead to running conditions. This should fix that.