Back to calculating distance by hand, optimized version was not working.
Also changed logging to be better for batch processing.