Algorithm 6 implemented