Added few pointer check conditions and missing cast (MSVC was crying)