Clean up output and change usage from echo to print
For caching state to work, G_mindist must be a static variable.