diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php
index 360938b88018cd6ac722dec51fb50187a3d24aea..6cbc5551ea72ee433ffc2ffca7b941f7312a1a49 100644
--- a/module/VuFind/src/VuFind/Config/Writer.php
+++ b/module/VuFind/src/VuFind/Config/Writer.php
@@ -100,32 +100,28 @@ class Writer
 
         // Process one line at a time...
         foreach ($lines as $line) {
-            // Once the setting is set, we can stop doing fancy processing -- it's
-            // just a matter of writing lines through unchanged:
-            if (!$settingSet) {
-                // Separate comments from content:
-                $parts = explode(';', trim($line), 2);
-                $content = trim($parts[0]);
-                $comment = isset($parts[1]) ? $parts[1] : '';
+            // Separate comments from content:
+            $parts = explode(';', trim($line), 2);
+            $content = trim($parts[0]);
+            $comment = isset($parts[1]) ? $parts[1] : '';
 
-                // Is this a section heading?
-                if (preg_match('/^\[(.+)\]$/', trim($content), $matches)) {
-                    // If we just left the target section and didn't find the
-                    // desired setting, we should write it to the end.
-                    if ($currentSection == $section && !$settingSet) {
-                        $line = $setting . ' = "' . $value . '"' . "\n\n" . $line;
-                        $settingSet = true;
-                    }
-                    $currentSection = $matches[1];
-                } else if (strstr($content, '=')) {
-                    list($key, $oldValue) = explode('=', $content, 2);
-                    if ($currentSection == $section && trim($key) == $setting) {
-                        $line = $setting . ' = "' . $value . '"';
-                        if (!empty($comment)) {
-                            $line .= ' ;' . $comment;
-                        }
-                        $settingSet = true;
+            // Is this a section heading?
+            if (preg_match('/^\[(.+)\]$/', trim($content), $matches)) {
+                // If we just left the target section and didn't find the
+                // desired setting, we should write it to the end.
+                if ($currentSection == $section && !$settingSet) {
+                    $line = $setting . ' = "' . $value . '"' . "\n\n" . $line;
+                    $settingSet = true;
+                }
+                $currentSection = $matches[1];
+            } else if (strstr($content, '=')) {
+                list($key, $oldValue) = explode('=', $content, 2);
+                if ($currentSection == $section && trim($key) == $setting) {
+                    $line = $setting . ' = "' . $value . '"';
+                    if (!empty($comment)) {
+                        $line .= ' ;' . $comment;
                     }
+                    $settingSet = true;
                 }
             }
 
diff --git a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
index 497394503ce7539adf4f876730e3cdcc5cb98caa..9cc770be200158271e7d5ca34fa0d7fd6b6a6aa8 100644
--- a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
+++ b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
@@ -110,4 +110,32 @@ class WriterTest extends \VuFindTest\Unit\TestCase
         $this->assertEquals('val2', $ini['test']['key2']);
         $this->assertEquals('val3', $ini['test']['key3']);
     }
+
+    /**
+     * Test setting a duplicate value.
+     *
+     * @return void
+     */
+    public function testSetDuplicateValue()
+    {
+        $cfg = "[test]\nkey1=val1\nkey1=val2\n";
+        $test = new Writer('fake.ini', $cfg);
+        $test->set('test', 'key1', 'val1b');
+        $ini = parse_ini_string($test->getContent(), true);
+        $this->assertEquals('val1b', $ini['test']['key1']);
+    }
+
+    /**
+     * Test that we add a missing section at the end if necessary.
+     *
+     * @return void
+     */
+    public function testAddMissingSection()
+    {
+        $cfg = "[test]\nkey1=val1\n";
+        $test = new Writer('fake.ini', $cfg);
+        $test->set('test2', 'key1', 'val1b');
+        $ini = parse_ini_string($test->getContent(), true);
+        $this->assertEquals('val1b', $ini['test2']['key1']);
+    }
 }
\ No newline at end of file