$dir can be undefined as far as I can see in case the code walks through line 15