Minor changes, refactoring, fixed distance bug