Properly check for nulls
Reorder logic to make the intention clearer