Refactored parts into Dijkstra