Fix bug by avoiding use of variables defined outside callbacks