Add residual costs and fix LZ encoder