FEL Gain Taking into Account Diffraction and Electron Beam Emittance; Generalized Madey's Theorem