added directory check with while loops etc