Make directory creation more robust