now algorithm is all right