added checks for existing directories