working algo in new version