Fix bugs in residual calculation for LZ