update residual computation and bug fixed. #2