new algorithm almost done