A CPAP apparatus having a flow generator (10, 12), a patient interface (18), an air delivery conduit (16) for delivering air from the flow generator to the patient interface, a pressure sensor (30) for determining the pressure from the flow generator, a flow sensor (28) for determining the flow from the flow generator, and a control mechanism (24) that causes air to be delivered through the air delivery conduit at a desired pressure at the patient interface. The control mechanism is configured to: receive sensed pressure data from the pressure sensor; receive sensed flow data from the flow sensor; determine an estimated mask pressure based on the sensed pressure data and the sensed flow data; calculate the derivative of flow at run time; set a converted derivative of flow value based on whether the value of the derivative of flow is positive or negative; calculate a pressure discrepancy value based on the converted derivative flow value; determine an adjusted mask pressure at the patient interface based on the estimated mask pressure and the pressure discrepancy value; and control the flow generator by maintaining the adjusted mask pressure substantially equal to the desired pressure.