added logic to check for and create output directory