made some change in algorithm