Added missing null checks and tests