The theory of spectral formation in thermal X$-$ray sources, where
the effects of Comptonization and Klein$-$Nishina corrections
are important, is presented.
Analytical expressions are obtained for
the produced spectrum
as a function of such input parameters as the
plasma temperature, the optical depth
of the plasma cloud and the injected soft photon spectrum.
The analytical theory developed here takes into account the dependence of
the scattering opacity on the photon energy.
It is shown that the plasma temperature as well as the
asymptotic rate of photon escape
from the plasma cloud determine the shape of the upscattered hard tail
in the emergent spectra, even in the case of very small optical depths.
The escape distributions of photons are
given for any optical depth of the plasma cloud
and their asymptotic for very small and large optical depths are examined.
The comparison of the new analytical theory with extensive Monte-Carlo
calculations are also presented. It is shown that this new generalized
approach matches extremely well the Monte-Carlo calculation in very wide
ranges of plasma temperature (1$-$500 keV) and plasma cloud optical
depths (0.1$-$10). The fits of spectra by the analytical Comptonization
model for a large variety of hard X$-$ray sources and determination of the
plasma temperature in the region of main energy release in Cyg X$-$1
and the Seyfert galaxy NGC 4151 are discussed.