LCS: factorize common code
LCS: more factorization