NULL pointer checking to avoid potential segfaults