Added logic to create output directory if doesn't exist.