Fixes #119. Add missing nil checks