rewriting clever algoritms