Add NULL pointer checks, fixes to remove compiler warnings in clang, MSC

Thanks to Marcus Froeschl for catching the NULL pointer cheks and testing the
compiler warning issues.