rewrote algo