refactor code and add distance score funciton