diff --git a/pae2xml.pl b/pae2xml.pl
index 5ea6a60cf72549198701520b08d7046f7c4975ab..83818e8ad62522e6f35270374588564ee0bb49c9 100755
--- a/pae2xml.pl
+++ b/pae2xml.pl
@@ -166,14 +166,6 @@ sub parse_pe {
     print "Writing $filename...\n";
     open(OUT, ">$filename");
 
-    my %fif=("", 0, "xF", 1, "xFC", 2, "xFCG",3, "xFCGD",4, "xFCGDA",5, "xFCGDAE",6, "xFCGDAEB",7, "bB",-1, "bBE",-2, "bBEA",-3, "bBEAD",-4, "bBEADG",-5, "bBEADGC",-6, "bBEADGCF",-7);
-    $keysig =~ s/(\s+)|&//gs;  # it is unclear what the & means, so we'll ignore it for now.
-    $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
-    $fifths = $fif{$keysig};
-    if ($fifths eq "") {
-      $fifths = "0";
-      print_error("Strange key signature '$keysig'.\n");
-    }
 
     print OUT '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
 <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
@@ -203,10 +195,8 @@ sub parse_pe {
 		<measure number="1">
 			<attributes>
 				<divisions>'.$divisions.'</divisions>
-				<key>
-					<fifths>'.$fifths.'</fifths>
-				</key>
-'.timesignature($timesig)
+'.keysignature ($keysig)
+.timesignature ($timesig)
 .clef ($clef)
 .'			</attributes>
 ';
@@ -362,17 +352,19 @@ sub parse_notes {
 				</time-modification>', $qq);
     } elsif ($notes =~ /^(%(\w(-|\+)\d))(.*)$/) {  # Clef change
       ($clef,$notes) = ($2,$4);
-      $clef = clef ($clef);
       print OUT "			<attributes>\n";
-      print OUT $clef;
+      print OUT clef ($clef);
       print OUT "			</attributes>\n";
     } elsif ($notes =~ /^@(\d\/\d|c\/?)\s*(.*)$/) {  # time signatue change
-      #	print "$notes\n";
       ($timesig,$notes) = ($1,$2);
-      #print "-> $timesig / $notes\n"; exit;
-      $timesig = timesignature($timesig);
-      print OUT "			<attributes>\n$timesig
-			</attributes>\n";
+      print OUT "			<attributes>\n";
+      print OUT timesignature($timesig);
+      print OUT "			</attributes>\n";
+    } elsif ($notes =~ /^\$((b|x)[ABCDEFG]*)\s*(.*)$/) {  # key signature change
+      ($keysig, $notes) = ($1, $3);
+      print OUT "			<attributes>\n";
+      print OUT keysignature ($keysig);
+      print OUT "			</attributes>\n";
     } elsif ($notes =~ /^(:?\/+:?)(.*)$/) {  # Barline (and repeats)
       $barline = $1;
       $notes = $2;
@@ -624,6 +616,27 @@ sub clef {
 ';
 }
 
+sub keysignature {
+  my ($keysig) = @_;
+
+  # TODO: How is a change to C major represented? by "$ " or "$x " or "$b "?
+  #       At the beginning, the $ part is left out, but mid-piece key changes
+  #       need to way to clear all accidentals! We accept all three cases above!
+  my %fif=("", 0, "x", 0, "b", 0, "xF", 1, "xFC", 2, "xFCG",3, "xFCGD",4, "xFCGDA",5, "xFCGDAE",6, "xFCGDAEB",7, "bB",-1, "bBE",-2, "bBEA",-3, "bBEAD",-4, "bBEADG",-5, "bBEADGC",-6, "bBEADGCF",-7);
+  $keysig =~ s/(\s+)|&//gs;  # it is unclear what the & means, so we'll ignore it for now.
+  $keysig =~ s/\[|\]//gs; # IGNORING brackets around a key sig.
+  $fifths = $fif{$keysig};
+  if ($fifths eq "") {
+    $fifths = "0";
+    print_error("Strange key signature '$keysig'.\n");
+  }
+  return '				<key>
+					<fifths>'.$fifths.'</fifths>
+				</key>
+';
+}
+
+
 sub timesignature {
   my ($timesig) = @_;
 
diff --git a/sample_files/KeySignature-change.pae b/sample_files/KeySignature-change.pae
new file mode 100644
index 0000000000000000000000000000000000000000..25ab6afe9a50df2f9c4a0026b7507f77f2d5e609
--- /dev/null
+++ b/sample_files/KeySignature-change.pae
@@ -0,0 +1,7 @@
+Test Composer
+Test case for key signature change
+1.1.1: S Key signatures
+plain&easy: %G-2@c$xFC� '1C/$bBEA 4FCAB/$x 4FCAB/8FCAB$bBEA 8FCAB/
+
+Test Library
+00000000
\ No newline at end of file