Added a null reference check