Added some checking for null objects.