changed distance function and sandberg solution implementation