The purpose of this study was to design an improved flattening filter for a Therac 20 medical linear accelerator. Profiles of the 18-MV x-ray beam produced by this accelerator measured along the diagonal of a 40 X 40 cm field at a depth of 5 cm were measured, and it was found that there were regions near the corners of the field where the dose was 109% of the central axis dose. An iterative algorithm for designing flattening filters was developed which required, as input, precise measurements of the following data: the unflattened primary beam profile, the fraction of the beam due to contamination radiation arising from interactions of primary photons with the flattening filter and the collimator assemblies, and the attenuation of the primary photons in water and lead as a function of angle from the central axis of the beam. A new flattening filter was designed and profiles of the beam were measured at a number of depths. These measurements showed that the beam was flattened to within +/- 1% out to 24 cm along the diagonal of a 40 X 40 cm field at a depth of 5 cm.