Protect context-lookups from undefined values

Fixes #166
Fixes #587