Added None type check to to_dict