Update algorithm with counting #166 and #167