Refactor: check if node is null (defensive programming)