moved adding dir to path after test of filename as @QuLogic pointed out.