Pathfinding issue

I'm having some issues with pathfinding, it works on simple paths, but more complex things become messed up. 

Click a tile to find the path to the selected tile. Hold and move mouse to create unwalkable tiles.

Creating a "cage" with just 1 opening is a good example where it foes wrong.