Using well-known correction techniques and algorithms, we have built a new procedure to correct the closed orbit of the CERN SPS and LEP. The procedure is driven by the input data, and no assumptions on the machine are made. Identical programs can therefore be used for both LEP and the SPS. Several algorithms can be used to correct the closed orbit, and additional features such as first-turn correction and harmonic analysis are incorporated in the procedure. Apart from necessary enhancements to the algorithms, we have defined a new data interface between the programs involved, to achieve high flexibility and language independence and to increase their efficiency. The correction procedure is described in detail and the performance is discussed using results both from the SPS during the lepton commissioning period and from the experience during the LEP startup.