Fixing undefined variable in JS