Completed algorithm