adapt algorithm for the new API