added some smarter directory detection