change my algo

inspired by jschendel, with the same thoughts but my method has bugs