new algo with debug option