Working solution with 5 random restarts