Improve null handling

Fixes #921