Improved the algorithm for generating migration numbers.