added failsafe method to store files in a directory