Fix to properly check for undefined. Also fix bug where 'lastTagent' was not being checked.