Fixed calculation of local error, but could be improved