fleshed out algorith, about to test it for the first time