LCS: factorize common code