Added option to create directories if needed