Algorithm improved