fix bug in calculating the error