Method for purifying wastewater and surface water, comprising pretreating the wastewater or surface water and subsequently feeding the pretreated wastewater or surface water to a biological filter which carries out denitrification of the water under substantially anoxic conditions, in which biological filter the water is fed in downward flow through a filtration unit which comprises a filter bed having granular activated carbon, GAC, as the filter medium, wherein said pretreating comprises: online measurement, in a first buffer vessel that is arranged upstream from the filter, of dissolved organic carbon, DOC, in the water in said first buffer vessel; characterized in that said pretreating comprises: setting an initial ozone setpoint in gr ozone / gr of DOC, based on the measured DOC, e.g. in the range of 0,4 to 1,2 gr ozone / gr of DOC, preferably 0,6 - 1,0 gr ozone / gr of DOC; feeding water from the first buffer vessel to an ozone reactor vessel arranged between the first buffer vessel and the biological filter; adding a dose of ozone to the ozone reactor vessel, wherein said dose corresponds to the ozone setpoint and is added below the water line in said ozone reactor vessel; feeding the water from the ozone reactor vessel to a second buffer vessel that is arranged between the ozone reactor vessel and the filter, measuring an ozone concentration in a gaseous effluent of the second buffer vessel and adjusting the ozone setpoint in dependence on the measured ozone concentration; and adding a carbon source to the wastewater or surface water between the ozone reactor vessel and the filter. online measurement, in a first buffer vessel that is arranged upstream from the filter, of dissolved organic carbon, DOC, in the water in said first buffer vessel; characterized in that said pretreating comprises: setting an initial ozone setpoint in gr ozone / gr of DOC, based on the measured DOC, e.g. in the range of 0,4 to 1,2 gr ozone / gr of DOC, preferably 0,6 - 1,0 gr ozone / gr of DOC; feeding water from the first buffer vessel to an ozone reactor vessel arranged between the first buffer vessel and the biological filter; adding a dose of ozone to the ozone reactor vessel, wherein said dose corresponds to the ozone setpoint and is added below the water line in said ozone reactor vessel; feeding the water from the ozone reactor vessel to a second buffer vessel that is arranged between the ozone reactor vessel and the filter, measuring an ozone concentration in a gaseous effluent of the second buffer vessel and adjusting the ozone setpoint in dependence on the measured ozone concentration; and adding a carbon source to the wastewater or surface water between the ozone reactor vessel and the filter. setting an initial ozone setpoint in gr ozone / gr of DOC, based on the measured DOC, e.g. in the range of 0,4 to 1,2 gr ozone / gr of DOC, preferably 0,6 - 1,0 gr ozone / gr of DOC; feeding water from the first buffer vessel to an ozone reactor vessel arranged between the first buffer vessel and the biological filter; adding a dose of ozone to the ozone reactor vessel, wherein said dose corresponds to the ozone setpoint and is added below the water line in said ozone reactor vessel; feeding the water from the ozone reactor vessel to a second buffer vessel that is arranged between the ozone reactor vessel and the filter, measuring an ozone concentration in a gaseous effluent of the second buffer vessel and adjusting the ozone setpoint in dependence on the measured ozone concentration; and adding a carbon source to the wastewater or surface water between the ozone reactor vessel and the filter.