improve crop algorithm