tweaked algorithm