Code improvement. Check array is not null before attempting to loop over it