Algorithm 2 added