beatrepeat: rewrite and comment algorithm