added directory creation if necessary