rebuilding algo