Reorder topology storage and improved the distance loop for performance