Fix distance bug, improve makefile