SAM ALGORITHM WORKING