refining algorithm