Updated to add directories if they don't exist