Edited algorithm