added check for null to prevent segmentation fault