finish implementing the contraction algorithm. This is unstable version