diff --git a/server.py b/server.py
index c20995db2a13af106c05ee542d47dde516fd3fca..a9dd795b9f696f66033abaee724c0abebd91ff8e 100644
--- a/server.py
+++ b/server.py
@@ -26,17 +26,17 @@ async def post_map(the_map: MapData):
 
 def write_map(map_data: MapData):
     dir_name = os.getenv("ESRI_OUTPUT_DIRECTORY", default=OUTPUT_DIR)
-    if not os.path.exists(dir_name):
+    if not os.path.exists(dir_name) or not os.path.isdir(dir_name):
         os.makedirs(dir_name, exist_ok=True)
     map_filename = path_join(dir_name, MAP_NAME)
     tag_filename = path_join(dir_name, TAG_LIST_NAME)
-    with open(map_filename, 'w') as map_file:
+    with open(map_filename, 'w') as map_file, \
+         open(tag_filename, 'w') as tag_file:
         map_file.write(map_data.esri)
-    with open(tag_filename, 'w') as tag_file:
         jdic = {
             "tags": map_data.arucos
         }
-        json.dump(jdic, tag_file)
+        json.dump(jdic, tag_file, indent=4)
     return True