Distance function support!

Implemented with a good tradeoff between performance and accuracy.
General beautifying old and ugly code

And standardize the decimal formatting for SQL with "." as the decimal
separator to prevent problems on (mainly) spanish servers