Fix distance calc bug. Better database generation.