Attempted fix of error where variable is undefined