edited algorithm