add nil condition to avoid nil.each_pair