diff --git a/pae2xml.pl b/pae2xml.pl
index 35dbf61d58775203617d5b74b8d9fc9af52c56ce..9ac3d5059f0084875777f857d82d2ef5f9ae1310 100755
--- a/pae2xml.pl
+++ b/pae2xml.pl
@@ -63,6 +63,11 @@ $old_duration = $divisions;
 $old_type = "";
 $old_octave = 4;
 
+# Store all alterations already used in the current measure, so that e.g.
+# xCC also detects the second note as a Cis! Needs to be reset at the
+# beginning of a new bar.
+%active_alterations = {};
+
 ($mday, $mon, $year) = (localtime()) [3..5];
 $encoding_date = sprintf("%4d-%02d-%02d", $year + 1900, $mon+1, $mday);
 
@@ -284,6 +289,7 @@ sub parse_notes {
 ';
         if ($n < $measrest) {
           print OUT "		</measure>\n";
+          reset_measure_attributes ();
           if ($notes ne "") {
             print OUT '		<measure number="'.$meas.'">
 ';
@@ -383,6 +389,7 @@ sub parse_notes {
 ';
       }
       print OUT "		</measure>\n";
+      reset_measure_attributes ();
       if ($notes ne "") {
 	print OUT '		<measure number="'.$meas.'">
 ';
@@ -442,6 +449,7 @@ sub parse_notes {
   }
   if ($mopen) {
     print OUT "		</measure>\n";
+    reset_measure_attributes ();
   }
 }
 
@@ -543,10 +551,24 @@ sub parse_note {
   $toprint .= "note: oct. $oct/acc. $acc/dur. $dur/dots $dot/grace,cue $gracecue/pitch $pitch\n";
 }
 
+sub reset_measure_attributes {
+  %active_alterations = {};
+  # TODO: reset all measure-only attributes, like manual accidentals
+}
+
 sub alter {
   my ($pitch, $acc, $keysig) = @_;
 
   my $alt = 0;
+  # If we had the same pitch with explicit alteration already in the current
+  # measure, that alteration goes on, unless the current note has an explicit
+  # alteration
+  if ($acc eq "") {
+    $acc = $active_alterations{$pitch};
+  } else {
+    # Store the explicit alteration of the current pitch!
+    $active_alterations{$pitch} = $acc;
+  }
 
   if (index ($keysig,$pitch) > -1) {
     $keysig =~ /^(.).*$/gs;
diff --git a/sample_files/Accidentals.pae b/sample_files/Accidentals.pae
new file mode 100644
index 0000000000000000000000000000000000000000..42d95ba1b3b52098c98f8d9fbd7d12cd7a42f891
--- /dev/null
+++ b/sample_files/Accidentals.pae
@@ -0,0 +1,7 @@
+Test Composer
+Accidentals (c-cis-cis-cisis-ces-ceses-ceses-c/ces-as-bes-g-cis-aisis-beses-g/)
+1.1.1: S accidentals
+plain&easy: %G-2@c$� '8CxCCxxCbCbbCCnC/$bBEADGC 8CABnGxCxxAbbBG/BAGCxBAGB/
+
+Test Library
+00000000
\ No newline at end of file