added check to ensure directory exists