Improved the whole algo