Add null checks where appropriate