making another adapted algorithm, algorithm4