diff --git a/monitor/discoveredcert.go b/monitor/discoveredcert.go
index 4023597..f6c07d6 100644
--- a/monitor/discoveredcert.go
+++ b/monitor/discoveredcert.go
@@ -12,7 +12,6 @@ package monitor
 import (
 	"bytes"
 	"encoding/hex"
-	"encoding/json"
 	"encoding/pem"
 	"fmt"
 	"strings"
@@ -49,7 +48,7 @@ func (cert *discoveredCert) pemChain() []byte {
 	return buffer.Bytes()
 }
 
-func (cert *discoveredCert) json() []byte {
+func (cert *discoveredCert) json() any {
 	object := map[string]any{
 		"tbs_sha256":    hex.EncodeToString(cert.TBSSHA256[:]),
 		"pubkey_sha256": hex.EncodeToString(cert.PubkeySHA256[:]),
@@ -65,21 +64,17 @@ func (cert *discoveredCert) json() []byte {
 		object["not_after"] = nil
 	}
 
-	jsonBytes, err := json.Marshal(object)
-	if err != nil {
-		panic(fmt.Errorf("encoding certificate as JSON failed unexpectedly: %w", err))
-	}
-	return jsonBytes
+	return object
 }
 
 func (cert *discoveredCert) save() error {
 	if err := writeFile(cert.CertPath, cert.pemChain(), 0666); err != nil {
 		return err
 	}
-	if err := writeFile(cert.JSONPath, cert.json(), 0666); err != nil {
+	if err := writeJSONFile(cert.JSONPath, cert.json(), 0666); err != nil {
 		return err
 	}
-	if err := writeFile(cert.TextPath, []byte(cert.Text()), 0666); err != nil {
+	if err := writeTextFile(cert.TextPath, cert.Text(), 0666); err != nil {
 		return err
 	}
 	return nil
diff --git a/monitor/fileutils.go b/monitor/fileutils.go
index 029172c..5d0ee28 100644
--- a/monitor/fileutils.go
+++ b/monitor/fileutils.go
@@ -12,6 +12,7 @@ package monitor
 import (
 	"crypto/rand"
 	"encoding/hex"
+	"encoding/json"
 	"fmt"
 	"os"
 )
@@ -36,6 +37,19 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
 	return nil
 }
 
+func writeTextFile(filename string, fileText string, perm os.FileMode) error {
+	return writeFile(filename, []byte(fileText), perm)
+}
+
+func writeJSONFile(filename string, data any, perm os.FileMode) error {
+	fileBytes, err := json.Marshal(data)
+	if err != nil {
+		return err
+	}
+	fileBytes = append(fileBytes, '\n')
+	return writeFile(filename, fileBytes, perm)
+}
+
 func fileExists(filename string) bool {
 	_, err := os.Lstat(filename)
 	return err == nil
diff --git a/monitor/statefile.go b/monitor/statefile.go
index c36908e..dcefe60 100644
--- a/monitor/statefile.go
+++ b/monitor/statefile.go
@@ -38,10 +38,5 @@ func loadStateFile(filePath string) (*stateFile, error) {
 }
 
 func (file *stateFile) store(filePath string) error {
-	fileBytes, err := json.Marshal(file)
-	if err != nil {
-		return err
-	}
-	fileBytes = append(fileBytes, '\n')
-	return writeFile(filePath, fileBytes, 0666)
+	return writeJSONFile(filePath, file, 0666)
 }
diff --git a/monitor/sthdir.go b/monitor/sthdir.go
index 9249f13..a499647 100644
--- a/monitor/sthdir.go
+++ b/monitor/sthdir.go
@@ -65,11 +65,7 @@ func storeSTHInDir(dirPath string, sth *ct.SignedTreeHead) error {
 	if fileExists(filePath) {
 		return nil
 	}
-	fileBytes, err := json.Marshal(sth)
-	if err != nil {
-		return err
-	}
-	return writeFile(filePath, fileBytes, 0666)
+	return writeJSONFile(filePath, sth, 0666)
 }
 
 func removeSTHFromDir(dirPath string, sth *ct.SignedTreeHead) error {