complete most of this algorithm