improved TERM handling