Modified lcs function