Improved handling of internal terminals in getvar() and getterm()