Added a null check in the if conditional