Handle base cases and null values gracefully