upgraded algorithm