Added checks for null pointers.