Fixed the iterative algorithm