Optimize variable lookups (bug #3541)