Added logic to ensure tmp directory exists and is writable