Added some much needed null pointer checks.