refactored dijkstra