Completed algorithm with mock