Rewrote with bidirectional search.