Refactored Main implementation to reduce code duplication for different starting points.