completed algorithm