Add further nil checks (fixes #862, ref #864)