Avoid crashing on name tests when name is empty.

Guard every occurrence of name.head with a test whether name.length > 0
Bring LambdaClass inline with its doc comment.

Avoid special treating TermRef, as this is not demanded by the
comment and does not change any of the test outcomes.
Improved documentation

Added explanations where suggested by Adriaan in his review.