Improve null checks; fix unit tests.