The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
Build.PL 212
Changes 061
MANIFEST 02
META.json 0105
META.yml 3447
Makefile.PL 22
README 12
lib/EBook/Tools/BISG.pm 0416
lib/EBook/Tools/EReader.pm 55
lib/EBook/Tools/IMP.pm 5332
lib/EBook/Tools/LZSS.pm 86
lib/EBook/Tools/MSReader.pm 22
lib/EBook/Tools/Mobipocket.pm 4884
lib/EBook/Tools/PalmDoc.pm 58
lib/EBook/Tools/Unpack.pm 1629
lib/EBook/Tools.pm 10555924
scripts/ebook.pl 82405
t/00-errorhandling.t 221
t/01-init_and_accessors.t 11
t/15-misc_procedures.t 226
t/20-unpack.t 4157
t/50-user_scripts.t 126
22 files changed (This is a version diff) 13717253
@@ -4,28 +4,38 @@ my $build = Module::Build->new
      module_name => 'EBook::Tools',
      license  => 'gpl2',
      requires => {
-         'perl'              => '5.8.8',
+         'perl'              => '5.10.1',
          'Archive::Zip'      => 1.16,
          'Bit::Vector'	     => 6.4,
          'Compress::Zlib'    => 1,
          'Config::IniFiles'  => 2,
          'Data::UUID'        => 1,
          'Date::Manip'       => 5.44,
+         'DBI'               => '1.614',
+         'Encode::Detect'    => 1,
          'File::MimeInfo'    => 0.13,
          'File::Slurp'	     => 9999,
+         'File::Which'       => 0,
 	 'HTML::Entities'    => 1,
          'HTML::TextToHTML'  => 2.44,
          'HTML::TreeBuilder' => 3.19,
          'Image::Size'       => 3.1,
+         'Lingua::EN::NameParse' => 1,
          'List::MoreUtils'   => 0.21,
+         'LWP::Protocol::https' => 0,
+         'Mojo::DOM'         => 0,	# Actually needs Mojolicious 4 or above
          'Palm::PDB'         => 1,
-         'Palm::Raw'         => 1, 
+         'Palm::Raw'         => 1,
          'String::CRC32'     => 1,
          'Tie::IxHash'       => 1.21,
          'Time::Local'       => 1.18,
+         'URI::Escape'	     => 1,
          'version'           => 0.74,
          'XML::Twig'         => 3.32,
      },
+     test_requires => {
+         'DBD::SQLite'          => '1.20',
+     },
      PL_files => { 'Bin.PL' => 'bin/ebook' },
      create_makefile_pl => 'small',
     );
@@ -1,6 +1,67 @@
 Release Notes
 =============
 
+0.5.4:
+ Bug fixes:
+  * Handle where a HOME environment variable has been set that doesn't
+    actually point to a directory.
+
+0.5.3:
+ Bug fixes:
+  * Automatically fix more duplicate subject codes
+  * Prevent some frequently breaking tests under automatic smoketesters
+
+0.5.2
+ Bug fixes:
+  * Minor POD display fixes
+
+0.5.1
+ Bug fixes:
+  * Fixes so perl as old as 5.10 really does still work
+  * Document dependencies properly in Build.PL (thanks to Syohei YOSHIDA)
+
+0.5.0
+ New Features:
+  * 'ebook convert' now provides one-shot conversion from any format
+    to epub with an optional fixup step in between.
+  * 'ebook fix' now provides subject and type normalization.  Library
+     of Congress normalization is still highly incomplete and is
+     expected to incrementally improve over time.
+  * 'ebook setmeta' now available to edit metadata fields.
+  * 'ebook setcover' now available to add/change cover images.
+  * 'ebook bisac' now available to search for or convert BISAC codes
+  * OPF files in subdirectories are now supported
+  * New class EBook::Tools::BISG to download BISAC information.
+  * New helper procedures 'system_result' and 'hashvalue_key_self'
+  * fix_guide() normalizes reference types
+  * Some advertisements are deleted from metadata automatically
+
+ Bug fixes:
+  * Lots of bugs in epub generation fixed, including automatic
+    generation of NCX files
+  * fix_links() no longer breaks when encountering mailto: news: or
+    backwards directory traversal links.
+  * fix_links() should no longer sometimes attempt to add the same
+    href multiple times.
+  * hrefs are decoded to give actual filesystem filenames
+  * XHTML 1.1 source files are no longer backwards-converted to XHTML
+    1.0
+  * Mobipocket UTF-8 HTML generation bugs fixed
+  * Fixed bugs that could cause Mobipocket filepos anchors to have
+    wrong ids
+  * 'ebook genepub' options now match the documentation
+  * OPF encoding is now autodetected instead of assumed as UTF-8
+  * Compatible with Perl 5.20
+
+ Behavior changes:
+  * Unpacking books other than .lit now causes the OPF filename to
+    default to 'content.opf' instead of a name based on the original
+    packed filename.
+  * fix_creators() is no longer automatically called from fix_misc()
+    as it may still get some exotic names wrong.
+  * The first argument in 'ebook genepub <arg>' now sets the output
+    file, not the input OPF.
+
 0.4.9
  New Features:
   * 'ebook unpack' now automatically handles ePub files (or to be more
@@ -5,6 +5,7 @@ README
 README.Helpers.txt
 Changes
 lib/EBook/Tools.pm
+lib/EBook/Tools/BISG.pm
 lib/EBook/Tools/EReader.pm
 lib/EBook/Tools/IMP.pm
 lib/EBook/Tools/LZSS.pm
@@ -217,3 +218,4 @@ t/us-constitution.lzss
 scripts/ebook.pl
 Makefile.PL
 META.yml
+META.json
@@ -0,0 +1,105 @@
+{
+   "abstract" : "Object class for manipulating and generating E-books",
+   "author" : [
+      "Zed Pobre <zed@debian.org>"
+   ],
+   "dynamic_config" : 1,
+   "generated_by" : "Module::Build version 0.421",
+   "license" : [
+      "gpl_2"
+   ],
+   "meta-spec" : {
+      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
+      "version" : "2"
+   },
+   "name" : "EBook-Tools",
+   "prereqs" : {
+      "configure" : {
+         "requires" : {
+            "Module::Build" : "0.42"
+         }
+      },
+      "runtime" : {
+         "requires" : {
+            "Archive::Zip" : "1.16",
+            "Bit::Vector" : "6.4",
+            "Compress::Zlib" : "1",
+            "Config::IniFiles" : "2",
+            "DBI" : "1.614",
+            "Data::UUID" : "1",
+            "Date::Manip" : "5.44",
+            "Encode::Detect" : "1",
+            "File::MimeInfo" : "0.13",
+            "File::Slurp" : "9999",
+            "File::Which" : "0",
+            "HTML::Entities" : "1",
+            "HTML::TextToHTML" : "2.44",
+            "HTML::TreeBuilder" : "3.19",
+            "Image::Size" : "3.1",
+            "LWP::Protocol::https" : "0",
+            "Lingua::EN::NameParse" : "1",
+            "List::MoreUtils" : "0.21",
+            "Mojo::DOM" : "0",
+            "Palm::PDB" : "1",
+            "Palm::Raw" : "1",
+            "String::CRC32" : "1",
+            "Tie::IxHash" : "1.21",
+            "Time::Local" : "1.18",
+            "URI::Escape" : "1",
+            "XML::Twig" : "3.32",
+            "perl" : "v5.10.1",
+            "version" : "0.74"
+         }
+      },
+      "test" : {
+         "requires" : {
+            "DBD::SQLite" : "1.20"
+         }
+      }
+   },
+   "provides" : {
+      "EBook::Tools" : {
+         "file" : "lib/EBook/Tools.pm",
+         "version" : "v0.5.4"
+      },
+      "EBook::Tools::BISG" : {
+         "file" : "lib/EBook/Tools/BISG.pm",
+         "version" : "v0.5.4"
+      },
+      "EBook::Tools::EReader" : {
+         "file" : "lib/EBook/Tools/EReader.pm",
+         "version" : "v0.5.2"
+      },
+      "EBook::Tools::IMP" : {
+         "file" : "lib/EBook/Tools/IMP.pm",
+         "version" : "v0.5.0"
+      },
+      "EBook::Tools::LZSS" : {
+         "file" : "lib/EBook/Tools/LZSS.pm",
+         "version" : "v0.5.0"
+      },
+      "EBook::Tools::MSReader" : {
+         "file" : "lib/EBook/Tools/MSReader.pm",
+         "version" : "v0.5.0"
+      },
+      "EBook::Tools::Mobipocket" : {
+         "file" : "lib/EBook/Tools/Mobipocket.pm",
+         "version" : "v0.5.0"
+      },
+      "EBook::Tools::PalmDoc" : {
+         "file" : "lib/EBook/Tools/PalmDoc.pm",
+         "version" : "v0.5.2"
+      },
+      "EBook::Tools::Unpack" : {
+         "file" : "lib/EBook/Tools/Unpack.pm",
+         "version" : "v0.5.0"
+      }
+   },
+   "release_status" : "stable",
+   "resources" : {
+      "license" : [
+         "http://opensource.org/licenses/gpl-2.0.php"
+      ]
+   },
+   "version" : "v0.5.4"
+}
@@ -2,61 +2,74 @@
 abstract: 'Object class for manipulating and generating E-books'
 author:
   - 'Zed Pobre <zed@debian.org>'
+build_requires:
+  DBD::SQLite: '1.20'
 configure_requires:
-  Module::Build: 0.36
-generated_by: 'Module::Build version 0.3607'
-license: gpl2
+  Module::Build: '0.42'
+dynamic_config: 1
+generated_by: 'Module::Build version 0.421, CPAN::Meta::Converter version 2.142060'
+license: gpl
 meta-spec:
   url: http://module-build.sourceforge.net/META-spec-v1.4.html
-  version: 1.4
+  version: '1.4'
 name: EBook-Tools
 provides:
   EBook::Tools:
     file: lib/EBook/Tools.pm
-    version: v0.4.9
+    version: v0.5.4
+  EBook::Tools::BISG:
+    file: lib/EBook/Tools/BISG.pm
+    version: v0.5.4
   EBook::Tools::EReader:
     file: lib/EBook/Tools/EReader.pm
-    version: v0.4.8
+    version: v0.5.2
   EBook::Tools::IMP:
     file: lib/EBook/Tools/IMP.pm
-    version: v0.4.8
+    version: v0.5.0
   EBook::Tools::LZSS:
     file: lib/EBook/Tools/LZSS.pm
-    version: v0.4.8
+    version: v0.5.0
   EBook::Tools::MSReader:
     file: lib/EBook/Tools/MSReader.pm
-    version: v0.4.8
+    version: v0.5.0
   EBook::Tools::Mobipocket:
     file: lib/EBook/Tools/Mobipocket.pm
-    version: v0.4.8
+    version: v0.5.0
   EBook::Tools::PalmDoc:
     file: lib/EBook/Tools/PalmDoc.pm
-    version: v0.4.8
+    version: v0.5.2
   EBook::Tools::Unpack:
     file: lib/EBook/Tools/Unpack.pm
-    version: v0.4.9
+    version: v0.5.0
 requires:
-  Archive::Zip: 1.16
-  Bit::Vector: 6.4
-  Compress::Zlib: 1
-  Config::IniFiles: 2
-  Data::UUID: 1
-  Date::Manip: 5.44
-  File::MimeInfo: 0.13
-  File::Slurp: 9999
-  HTML::Entities: 1
-  HTML::TextToHTML: 2.44
-  HTML::TreeBuilder: 3.19
-  Image::Size: 3.1
-  List::MoreUtils: 0.21
-  Palm::PDB: 1
-  Palm::Raw: 1
-  String::CRC32: 1
-  Tie::IxHash: 1.21
-  Time::Local: 1.18
-  XML::Twig: 3.32
-  perl: v5.8.8
-  version: 0.74
+  Archive::Zip: '1.16'
+  Bit::Vector: '6.4'
+  Compress::Zlib: '1'
+  Config::IniFiles: '2'
+  DBI: '1.614'
+  Data::UUID: '1'
+  Date::Manip: '5.44'
+  Encode::Detect: '1'
+  File::MimeInfo: '0.13'
+  File::Slurp: '9999'
+  File::Which: '0'
+  HTML::Entities: '1'
+  HTML::TextToHTML: '2.44'
+  HTML::TreeBuilder: '3.19'
+  Image::Size: '3.1'
+  LWP::Protocol::https: '0'
+  Lingua::EN::NameParse: '1'
+  List::MoreUtils: '0.21'
+  Mojo::DOM: '0'
+  Palm::PDB: '1'
+  Palm::Raw: '1'
+  String::CRC32: '1'
+  Tie::IxHash: '1.21'
+  Time::Local: '1.18'
+  URI::Escape: '1'
+  XML::Twig: '3.32'
+  perl: v5.10.1
+  version: '0.74'
 resources:
   license: http://opensource.org/licenses/gpl-2.0.php
-version: v0.4.9
+version: v0.5.4
@@ -1,5 +1,5 @@
-# Note: this file was auto-generated by Module::Build::Compat version 0.3607
-require 5.008008;
+# Note: this file was auto-generated by Module::Build::Compat version 0.4210
+require 5.010001;
     use Module::Build::Compat 0.02;
     
     Module::Build::Compat->run_build_pl(args => \@ARGV);
@@ -23,7 +23,7 @@ DEPENDENCIES
 
 This module requires these other modules and libraries:
 
- Perl 5.8.8 or higher
+ Perl 5.10.0 or higher
  Archive::Zip
  Bit::Vector
  Compress::Zlib
@@ -40,6 +40,7 @@ This module requires these other modules and libraries:
  Image::Size
  List::MoreUtils
  Module::Build
+ Mojo::DOM
  P5-Palm
   * This may not be found by the CPAN builder.  If you have trouble
     with the installation, try downloading it from the CPAN website
@@ -0,0 +1,416 @@
+package EBook::Tools::BISG;
+use warnings; use strict; use utf8;
+use 5.010;
+use version 0.74; our $VERSION = qv("0.5.4");
+
+=head1 NAME
+
+EBook::Tools::BISG - Class for downloading information from the Book Industry Study Group webpages
+
+=head1 SYNOPSIS
+
+ use EBook::Tools::BISG;
+ my $bisg = EBook::Tools::BISG->new();
+ $bisg->download_bisac;
+ $bisg->save_bisac;
+ say $bisg->bisac('fic000000');
+ %bisac_codes = $bisg->bisac();
+
+=head1 DEPENDENCIES
+
+=over
+
+=item * C<DBI>
+
+This will also require a DBD of your choice.  The default is to use a
+local SQLite store.
+
+=item * C<LWP>
+
+=item * C<Mojo::DOM>
+
+=back
+
+=cut
+
+use Carp;
+use DBI;
+use EBook::Tools qw(:all);
+use LWP;
+use Mojo::DOM;
+
+
+#################################
+########## CONSTRUCTOR ##########
+#################################
+
+=head1 CONSTRUCTOR
+
+=head2 C<new(%args)>
+
+Instantiates a new Ebook::Tools::BISG object.
+
+=head3 Arguments
+
+=over
+
+=item * C<baseurl>
+
+The base url of the bisg.org website or mirror to use.
+
+=item * C<dsn>
+
+The Perl::DBI Data Source Name.  Defaults to a sqlite store named
+bisac.sqlite in the user config directory.
+
+=back
+
+=cut
+
+my @fields = (
+    'baseurl',
+    'bisac_codes',
+    'dbuser',
+    'dbpass',
+    'dsn',
+    );
+
+require fields;
+fields->import(@fields);
+
+sub new {
+    my ($self,%args) = @_;
+    my $class = ref($self) || $self;
+    my $subname = (caller(0))[3];
+    debug(2,"DEBUG[",$subname,"]");
+
+    my %valid_args = (
+        'baseurl' => 1,
+        'dbuser'  => 1,
+        'dbpass'  => 1,
+        'dsn'     => 1,
+        );
+    foreach my $arg (keys %args) {
+        croak($subname,"(): invalid argument '",$arg,"'")
+          if(!$valid_args{$arg});
+    }
+
+    $self = fields::new($class);
+    $self->{baseurl} = $args{file} || 'http://www.bisg.org';
+    $self->{bisac_codes} = {};
+    $self->{dbuser} = $args{dbuser} || '';
+    $self->{dbpass} = $args{dbpass} || '';
+
+    if($args{dsn}) {
+        $self->{dsn} = $args{dsn};
+        $self->load_bisac();
+    }
+    else {
+        my $configdir = userconfigdir();
+        if($configdir) {
+            $self->{dsn} = 'dbi:SQLite:dbname=' . $configdir . '/bisac.sqlite';
+            $self->load_bisac();
+        }
+    }
+
+    return $self;
+}
+
+
+=head1 ACCESSOR METHODS
+
+=head2 C<bisac($code)>
+
+Returns either the name matching a particular code (case-insensitive),
+or the hash of all BISAC codes and references with the keys in
+lower-case if no argument is provided.
+
+=cut
+
+sub bisac :method
+{
+    my ($self,$code) = @_;
+    my $subname = ( caller(0) )[3];
+
+    if ($code) {
+        return $self->{bisac_codes}->{$code};
+    }
+    return %{$self->{bisac_codes}};
+}
+
+
+=head2 C<find($regexp)>
+
+Returns a list of all BISAC values (names) where either the key or the
+value for that entry matches a case-insensitive regular expression.
+If no argument is specified, or it is just '.', then the entire list
+is returned.
+
+=cut
+
+sub find :method
+{
+    my ($self,$regexp) = @_;
+    my $subname = ( caller(0) )[3];
+
+    my %seen;
+    my @keys;
+
+    # Create a list of all unique keys and values (lowercased to be
+    # used again as keys)
+    foreach my $key (keys %{$self->{bisac_codes}}) {
+        $seen{$key} = 1;
+    }
+    foreach my $value (values %{$self->{bisac_codes}}) {
+        $seen{lc $value} = 1;
+    }
+
+    if(not $regexp or $regexp eq '.') {
+        @keys = sort keys %seen;
+    }
+    else {
+        @keys = sort grep { /$regexp/i } keys %seen;
+    }
+
+    return @{$self->{bisac_codes}}{@keys};
+}
+
+
+=head1 MODIFIER METHODS
+
+=head2 C<download_bisac()>
+
+Downloads the BISAC codes from the BISG website and converts them into
+a hash.  Codes and obsolete entries are lowercased and made the keys,
+the values will be the official names they map to.
+
+=cut
+
+sub download_bisac :method
+{
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+
+    # LWP/Cookies variables
+    my $browser = LWP::UserAgent->new;
+    my $content;
+    my $content_type;
+    my $response;
+    my $url;
+
+    # HTML parsing
+    my $dom = Mojo::DOM->new();
+    my @elements;
+    my $category;
+    my $key;
+    my $value;
+    my $bisac_headings_top_url =
+      $self->{baseurl} . '/complete-bisac-subject-headings-2013-edition';
+
+    debug(2,"DEBUG[",$subname,"]");
+
+
+    $response = $browser->get($bisac_headings_top_url);
+    croak "Error loading ${bisac_headings_top_url}: ", $response->status_line()
+      unless $response->is_success();
+    $content_type = $response->content_type();
+    $content = $response->content();
+
+    $dom->parse($content);
+
+    foreach my $link ($dom->find('a[href*="bisac-subject-headings-list-"]')->each) {
+        my $href = $link->attr('href');
+        my $url = $self->{baseurl} . $href;
+        $response = $browser->get($url);
+        croak "Error loading ${url}: ", $response->status_line()
+          unless $response->is_success();
+        $content_type = $response->content_type();
+        $content = $response->content();
+
+        $dom->parse($content);
+
+        $category = uc $dom->at('h3')->text;
+        say "parsing ${category}...";
+
+        foreach my $tr ($dom->find('tr')->each) {
+            my $td = $tr->children('td');
+            if ($td->[1] and $td->[1] =~ /\w/) {
+                $key = $td->[0]->all_text;
+                if ($key =~ /\w/) {
+                    $value = $td->[1]->all_text;
+                }
+                else {
+                    # The first column is empty, but the second column
+                    # is not.  This indicates a non-code reference
+                    # entry.
+                    #
+                    # If the second column does not contain ' see ',
+                    # then we don't know how to parse the reference,
+                    # and we skip.  If it contains ' see ' and ' or '
+                    # then the reference is ambiguous and we also
+                    # skip.
+                    #
+                    # If it contains 'headings under' then the result
+                    # is ambiguous and we must skip.
+                    #
+                    # Otherwise the portion before ' see ' becomes the
+                    # key and the portion afterwards becomes the
+                    # value.
+
+                    if ($td->[1]->all_text =~
+                          /^				# Start of string
+                           (?! .* headings \s under)	# Not matching look-ahead 'headings under'
+                           (.*?) \s see \s		# Match all characters up to ' see '
+                           ( (?:(?! \s or \s )		# Complex match not matching ' or '
+                                   .)*)			# ... but matching anything else
+                          /x) {
+
+                        $key = $1;
+                        $value = $2;
+
+                        # The value won't have the category prefix if
+                        # it's in the current category, so we need to
+                        # check and prepend if necessary.
+                        if ($value !~ /^[A-Z]{3}/x) {
+                            $value = $category . ' / ' . $value;
+                        }
+                    }
+                    else {
+                        $key = '';
+                        $value = '';
+                    }
+                }
+                # Remove trailing parentheticals and asterisks
+                $value =~ s/(.*?) \(.*\)/$1/;
+                $value =~ s/(.*?) \*/$1/;
+
+                if($key and $value) {
+                    $key = lc $key;
+                    $self->{bisac_codes}->{$key} = $value;
+
+                    # Also put in a self-mapping of the lowercased
+                    # value to itself if one does not already exist.
+                    if (! $self->{bisac_codes}->{lc $value}) {
+                        $self->{bisac_codes}->{lc $value} = $value;
+                    }
+                }
+            }
+            else {
+                # There is one row with a single <td> element at the
+                # very end.  We ignore this.
+            }
+        }
+    }
+    return;
+}
+
+
+=head2 C<load_bisac()>
+
+Load the BISAC codes from the specified database into the hash.  This
+is called automatically by new().
+
+If the load fails, nothing will happen (silently!), so the only way to
+tell is to check to see if you have a populated hash afterwards.
+
+=cut
+
+sub load_bisac :method
+{
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+
+    my $dsn = $self->{dsn};
+    my $sql;
+
+    my $dbh = DBI->connect(
+        $self->{dsn},$self->{dbuser},$self->{dbpass}, {
+            AutoCommit => 0,
+            PrintError => 0,
+            RaiseError => 0,
+            sqlite_see_if_its_a_number => 1,
+        });
+    return unless $dbh;
+
+    my $sth;
+    my $row;
+
+    $sth = $dbh->prepare('SELECT code,name FROM bisac_codes');
+    if (! $sth) {
+        # Table doesn't exist
+        $dbh->disconnect;
+        return;
+    }
+    $sth->execute() or croak ("Failed to load BISAC codes from database");
+    while($row = $sth->fetchrow_arrayref) {
+        $self->{bisac_codes}->{$row->[0]} = $row->[1];
+    }
+
+    $dbh->disconnect();
+
+    return;
+}
+
+
+=head2 C<save_bisac()>
+
+Save the BISAC codes in the hash to the database.  This will destroy
+any existing table named bisac_codes.
+
+Croaks on failure.
+
+=cut
+
+sub save_bisac :method
+{
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+
+    my $dbh = DBI->connect(
+        $self->{dsn},$self->{dbuser},$self->{dbpass}, {
+            AutoCommit => 0,
+            PrintError => 0,
+            RaiseError => 1,
+            sqlite_see_if_its_a_number => 1,
+        })
+      or croak("Failed to connect to $self->{dsn}");
+
+    my $sth;
+    my $sql;
+    my @fields;
+    my $fieldlist;
+    my $placeholders;
+
+    # Ensure the bisac_codes table exists as a blank slate.
+    $dbh->{RaiseError} = 0;
+    $sth = $dbh->prepare('SELECT * FROM bisac_codes WHERE 0 = 1');
+    $dbh->{RaiseError} = 1;
+    if ($sth) {
+        $dbh->do('DROP TABLE bisac_codes');
+    }
+
+    $sql = '
+      CREATE TABLE bisac_codes (
+        code	text	PRIMARY KEY,
+        name	text,
+        comment	text
+      );';
+    $sth = $dbh->do($sql)
+      or croak ("Failed to create bisac_codes table!");
+    $dbh->commit();
+
+    @fields = ('code','name');
+    $fieldlist = join (', ',@fields);
+    $placeholders = join (', ', map {'?'} @fields);
+    $sth = $dbh->prepare("INSERT INTO bisac_codes (${fieldlist}) VALUES (${placeholders})");
+    foreach my $key (keys %{$self->{bisac_codes}}) {
+        $sth->execute($key,$self->{bisac_codes}->{$key});
+    }
+    $dbh->commit;
+    $dbh->disconnect;
+
+    return;
+}
+
+1;
+__END__
@@ -1,6 +1,6 @@
 package EBook::Tools::EReader;
 use warnings; use strict; use utf8;
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.2");
 
 # Double-sigils are needed for lexical variables in clear print statements
 ## no critic (Double-sigil dereference)
@@ -396,7 +396,7 @@ sub sidebars_html
 Writes the raw book text to disk in PML form (including all sidebars
 and footnotes) with the given filename.
 
-If C<$filename> is not specified, writes to C<$self->filebase> with
+If C<$filename> is not specified, writes to C<< $self->filebase >> with
 a ".html" extension.
 
 Returns the filename used on success, or undef if there was no text to
@@ -416,7 +416,7 @@ sub write_html :method
 
     debug(1,"DEBUG: writing HTML text to '",$filename,"'");
 
-    open(my $fh,">:utf8",$filename)
+    open(my $fh,'>:encoding(UTF-8)',$filename)
         or croak($subname,"(): unable to open '",$filename,"' for writing!\n");
     print {*$fh} $self->html;
     close($fh)
@@ -469,7 +469,7 @@ sub write_images :method
 Writes the raw book text to disk in PML form (including all sidebars
 and footnotes) with the given filename.
 
-If C<$filename> is not specified, writes to C<$self->filebase> with
+If C<$filename> is not specified, writes to C<< $self->filebase >> with
 a ".pml" extension.
 
 Returns the filename used on success, or undef if there was no text to
@@ -542,7 +542,7 @@ sub write_unknown_records :method
 
 =head2 C<Load($filename)>
 
-Sets C<$self->{filename}> and then loads and parses the file specified
+Sets C<< $self->{filename} >> and then loads and parses the file specified
 by C<$filename>, calling L</ParseRecord(%record)> on every record
 found.
 
@@ -1,14 +1,13 @@
 package EBook::Tools::IMP;
 use warnings; use strict; use utf8;
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.0");
 
-# Perl Critic overrides:
-## no critic (Package variable)
+## Perl Critic overrides:
 # RequireBriefOpen seems to be way too brief to be useful
 ## no critic (RequireBriefOpen)
-# Double-sigils are needed for lexical filehandles in clear print statements
-## no critic (Double-sigil dereference)
+# We're not interpolating constants and not making them lexical makes identifying them easier
+## no critic (ProhibitConstantPragma)
 
 =head1 NAME
 
@@ -42,7 +41,7 @@ use File::Basename qw(basename dirname fileparse);
 use File::Path;     # Exports 'mkpath' and 'rmtree'
 use Image::Size;
 use List::MoreUtils qw(any none);
-binmode(STDERR,":utf8");
+binmode(STDERR,':encoding(UTF-8)');
 
 my $drmsupport = 0;
 eval
@@ -163,13 +162,12 @@ Returns 1 on success, or undef on failure.
 
 sub load :method
 {
-    my $self = shift;
-    my ($filename) = @_;
+    my ($self,$filename) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
 
-    if(!$self->{filename} and !$filename)
+    if(not $self->{filename} and not $filename)
     {
         carp($subname,"(): no filename specified!\n");
         return;
@@ -287,8 +285,7 @@ attributes.  Returns 1 on success, or undef on failure.
 
 sub load_resdir
 {
-    my $self = shift;
-    my ($dirname) = @_;
+    my ($self,$dirname) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -400,7 +397,7 @@ sub load_resdir
         return;
     }
 
-    my @filelist = <*>;
+    my @filelist = glob '*';
     $self->{resources} = {};
     $self->{toc} = ();
     foreach my $file (@filelist)
@@ -576,8 +573,7 @@ undisplayable PICT image.
 
 sub find_image_type :method
 {
-    my $self = shift;
-    my ($id,@excluded) = @_;
+    my ($self,$id,@excluded) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -605,8 +601,7 @@ Returns undef if no match was found or a name was not specified.
 
 sub find_resource_by_name :method
 {
-    my $self = shift;
-    my ($name) = @_;
+    my ($self,$name) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -638,8 +633,7 @@ not valid, or if C<$id> is not provided.
 
 sub image :method
 {
-    my $self = shift;
-    my ($type,$id) = @_;
+    my ($self,$type,$id) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -706,8 +700,7 @@ specific ID is returned, rather than the entire hash of hashrefs.
 
 sub image_hashref :method
 {
-    my $self = shift;
-    my ($type,$id) = @_;
+    my ($self,$type,$id) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -745,8 +738,7 @@ specified.
 
 sub image_ids :method
 {
-    my $self = shift;
-    my ($type) = @_;
+    my ($self,$type) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -799,8 +791,7 @@ element exists.
 
 sub offsetelement :method
 {
-    my $self = shift;
-    my ($offset) = @_;
+    my ($self,$offset) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -901,7 +892,7 @@ sub pack_imp_header :method
 
     $header = pack('n',$self->{version});
     $header .= 'BOOKDOUG';
-    if(! $self->{unknown0x0a}
+    if(not $self->{unknown0x0a}
        or length($self->{unknown0x0a}) != 8)
     {
         carp($subname,"():\n",
@@ -963,8 +954,7 @@ the name is only used if the type lookup fails.
 
 sub pack_imp_resource :method
 {
-    my $self = shift;
-    my %args = @_;
+    my ($self,%args) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -978,7 +968,7 @@ sub pack_imp_resource :method
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
-    if(!$args{name} and !$args{type})
+    if(not $args{name} and not $args{type})
     {
         carp($subname,"():\n",
              " at least one of name or type must be specified!\n");
@@ -989,10 +979,10 @@ sub pack_imp_resource :method
     my $resource;
     my $resdata;
 
-    if(!($type and $self->{resources}->{$type}) and $args{name})
+    if(not ($type and $self->{resources}->{$type}) and $args{name})
     {
         $type = $self->find_resource_by_name($args{name});
-        if(!$type or !$self->{resources}->{$type})
+        if(not $type or not $self->{resources}->{$type})
         {
             carp($subname,"():\n",
                  " no resource with name '",$args{name},"' found!\n");
@@ -1239,8 +1229,7 @@ is not found.
 
 sub resource :method
 {
-    my $self = shift;
-    my ($type) = @_;
+    my ($self,$type) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1309,8 +1298,7 @@ to that TOC entry, if it exists, or undef otherwise.
 
 sub tocentry :method
 {
-    my $self = shift;
-    my ($index) = @_;
+    my ($self,$index) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1359,8 +1347,7 @@ directory (see also L</resdirname()>).
 
 sub write_images :method
 {
-    my $self = shift;
-    my %args = @_;
+    my ($self,%args) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1424,8 +1411,7 @@ filename) was invalid or missing, or the file could not be written.
 
 sub write_imp :method
 {
-    my $self = shift;
-    my ($filename) = @_;
+    my ($self,$filename) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1444,7 +1430,7 @@ sub write_imp :method
     my $bookpropdata = $self->pack_imp_book_properties();
     my $tocdata = $self->pack_imp_toc;
 
-    if(!$headerdata or length($headerdata) != 48)
+    if(not $headerdata or length($headerdata) != 48)
     {
         carp($subname,"(): invalid header data!\n");
         return;
@@ -1581,8 +1567,7 @@ will be carped and the method will return undef.
 
 sub write_text :method
 {
-    my $self = shift;
-    my %args = @_;
+    my ($self,%args) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1719,8 +1704,7 @@ in key C<unknown2>.  This value may not be present at all.
 
 sub parse_eti_server_data :method
 {
-    my $self = shift;
-    my ($data) = @_;
+    my ($self,$data) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1808,8 +1792,7 @@ of the data passed.
 
 sub parse_imp_book_properties :method
 {
-    my $self = shift;
-    my ($propdata) = @_;
+    my ($self,$propdata) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1948,8 +1931,7 @@ caution -- this value may be renamed if more information is obtained.
 
 sub parse_imp_header :method
 {
-    my $self = shift;
-    my ($headerdata) = @_;
+    my ($self,$headerdata) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -2628,8 +2610,7 @@ Size of the resource data in bytes.  Stored in hash key C<size>.
 
 sub parse_imp_toc_v1 :method
 {
-    my $self = shift;
-    my ($tocdata) = @_;
+    my ($self,$tocdata) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -2712,8 +2693,7 @@ Unknown, but always either zero or one.  Stored in C<unknown2>.
 
 sub parse_imp_toc_v2 :method
 {
-    my $self = shift;
-    my ($tocdata) = @_;
+    my ($self,$tocdata) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -2820,8 +2800,7 @@ must be blank.
 
 sub set_book_properties :method
 {
-    my $self = shift;
-    my %args = @_;
+    my ($self,%args) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure!\n") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1,14 +1,13 @@
 package EBook::Tools::LZSS;
 use warnings; use strict; use utf8;
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.0");
 
-# Perl Critic overrides:
-## no critic (Package variable)
+## Perl Critic overrides:
 # RequireBriefOpen seems to be way too brief to be useful
 ## no critic (RequireBriefOpen)
-# Double-sigils are needed for lexical filehandles in clear print statements
-## no critic (Double-sigil dereference)
+# We're not interpolating constants and not making them lexical makes identifying them easier
+## no critic (ProhibitConstantPragma)
 
 =head1 NAME
 
@@ -39,7 +38,7 @@ use EBook::Tools qw(:all);
 use Encode;
 use File::Basename qw(dirname fileparse);
 use File::Path;     # Exports 'mkpath' and 'rmtree'
-binmode(STDERR,":utf8");
+binmode(STDERR,':encoding(UTF-8)');
 
 
 use constant ENCODED => 0;
@@ -208,8 +207,7 @@ string and returns a reference to the uncompressed string.
 
 sub uncompress :method
 {
-    my $self = shift;
-    my ($dataref) = @_;
+    my ($self,$dataref) = @_;
     my $subname = (caller(0))[3];
     debug(2,"DEBUG[",$subname,"]");
 
@@ -1,7 +1,7 @@
 package EBook::Tools::MSReader;
 use warnings; use strict; use utf8;
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.0");
 
 # Perl Critic overrides:
 ## no critic (Package variable)
@@ -44,7 +44,7 @@ use EBook::Tools qw(debug userconfigdir);
 use Encode;
 use File::Basename qw(dirname fileparse);
 use File::Path;     # Exports 'mkpath' and 'rmtree'
-binmode(STDERR,":utf8");
+binmode(STDERR,':encoding(UTF-8)');
 
 my $drmsupport = 0;
 eval
@@ -4,7 +4,7 @@ use warnings; use strict; use utf8;
 #use 5.010; # Needed for smart-match operator
 #v5.10 feature use removed until 5.10 is standard on MacOSX and Debian
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.0");
 
 # Perl Critic overrides:
 ## no critic (Package variable)
@@ -494,13 +494,12 @@ sub write_text :method
     return unless($$self{text});
 
     debug(1,"DEBUG: writing text to '",$filename,
-          "', encoding ",$pdbencoding{$$self{encoding}});
+          "', encoding ",$pdbencoding{$self->{encoding}});
 
     open(my $fh,">",$filename)
         or croak($subname,"(): unable to open '",$filename,"' for writing!\n");
-    if($$self{encoding} == 65001) { binmode($fh,":utf8"); }
-    else { binmode($fh); }
-    print {*$fh} $$self{text};
+    binmode($fh);
+    print {*$fh} $self->{text};
     close($fh)
         or croak($subname,"(): unable to close '",$filename,"'!\n");
 
@@ -1222,7 +1221,7 @@ sub ParseRecordText :method
     my $recordtext;
     my $extradatasize;
 
-    debug(1,"DEBUG: extradataflags = ",$$self{header}{mobi}{extradataflags});
+    debug(3,"DEBUG: extradataflags = ",$$self{header}{mobi}{extradataflags});
     if( $$self{header}{mobi}{extradataflags} )
     {
         $extradatasize = record_extradata_size(
@@ -1337,6 +1336,8 @@ sub fix_html :method   ## no critic (Always unpack @_ first)
 
     my $tree;
     my @elements;
+    my $head_element;
+    my $element;
     my $recindex;
     my $link;
 
@@ -1347,31 +1348,59 @@ sub fix_html :method   ## no critic (Always unpack @_ first)
     $self->fix_html_filepos();
 
     # Convert or remove the Mobipocket-specific tags
-    $$self{text} =~ s#<mbp:pagebreak [\s\n]*
-                     #<br style="page-break-after: always" #gix;
-    $$self{text} =~ s#</mbp:pagebreak>##gix;
-    $$self{text} =~ s#</?mbp:nu>##gix;
-    $$self{text} =~ s#</?mbp:section>##gix;
-    $$self{text} =~ s#</?mbp:frameset##gix;
-    $$self{text} =~ s#</?mbp:slave-frame##gix;
+    $self->{text} =~ s#<mbp:pagebreak [\s\n]*
+                      #<br style="page-break-after: always" #gix;
+    $self->{text} =~ s#</mbp:pagebreak>##gix;
+    $self->{text} =~ s#</?mbp:nu>##gix;
+    $self->{text} =~ s#</?mbp:section>##gix;
+    $self->{text} =~ s#</?mbp:frameset##gix;
+    $self->{text} =~ s#</?mbp:slave-frame##gix;
 
 
     # More complex alterations will require a HTML tree
     $tree = HTML::TreeBuilder->new();
     $tree->ignore_unknown(0);
+    $tree->no_expand_entities(1);
+    $tree->store_comments(1);
 
-    # If the encoding is UTF-8, we have to decode it before the parse
-    # or the parser will break
-    if($encoding == 65001)
-    {
-        debug(1,"DEBUG: decoding utf8 text");
-        croak($subname,"(): failed to decode UTF-8 text")
-            unless(decode_utf8($$self{text}));
-        $tree->parse($$self{text});
+    # If the encoding is UTF-8, we have to note it specially before
+    # the parse or the parser will break
+    if($encoding == 65001) {
+        $tree->utf8_mode(1);
     }
-    else { $tree->parse($$self{text}); }
+    $tree->parse($self->{text});
     $tree->eof();
 
+    # Update contents of <head>
+    $head_element = $tree->find('head');
+    if(! $head_element) {
+        $self->add_warning("No <head> element found in Mobipocket HTML!");
+    }
+    else {
+        # Fix missing <title>
+        $element = $head_element->find('title');
+        if(! $element) {
+            debug(1,"DEBUG: title element missing, creating as '",
+                  $self->{title},"'");
+            $head_element->push_content(['title',$self->{title}]);
+        }
+        else {
+            if(! $element->as_trimmed_text) {
+                debug(1,"DEBUG: title is empty, setting to '",$self->{title},"'");
+                $element->replace_with($self->{title});
+            }
+        }
+
+        # Set UTF8 content type if necessary
+        if($encoding == 65001) {
+            $head_element->push_content(
+                [ 'meta',
+                {'http-equiv' => 'Content-type'},
+                {'content' => 'text/html;charset=UTF-8' },
+                 ]);
+        }
+    }
+
     # Replace img recindex links with img src links
     debug(2,"DEBUG: converting img recindex attributes");
     @elements = $tree->find('img');
@@ -1402,36 +1431,43 @@ sub fix_html :method   ## no critic (Always unpack @_ first)
     }
 
     debug(2,"DEBUG: converting HTML tree");
-    $$self{text} = $tree->as_HTML;
+    # We don't bother specifying an indent here, as the resulting
+    # formatting is still broken and we have to insert newlines later
+    # anyway.
+    #
+    # TODO: experiment with using Mojo::DOM instead
+    $self->{text} = $tree->as_HTML(undef,'',{});
 
     croak($subname,"(): HTML tree output is empty")
-        unless($$self{text});
+        unless($self->{text});
 
     # Strip embedded nulls
     debug(2,"DEBUG: stripping nulls");
-    $$self{text} =~ s/\0//gx;
+    $self->{text} =~ s/\0//gx;
 
-    # HTML::TreeBuilder will remove all of the newlines, so add some
-    # back in for readability even if tidy isn't called
+    # HTML::TreeBuilder will not insert all appropriate newlines even
+    # specifying indents in as_HTML, so add some back in for
+    # readability even if tidy isn't called
+    #
     # This is unfortunately quite slow.
-    unless($args{nonewlines})
-    {
+    unless($args{nonewlines}) {
         debug(1,"DEBUG: adding newlines");
-        $$self{text} =~ s#<(body|html)> \s* \n?
-                         #<$1>\n#gix;
-        $$self{text} =~ s#\n? <div
-                         #\n<div#gix;
-        $$self{text} =~ s#</div> \s* \n?
-                         #</div>\n#gix;
-        $$self{text} =~ s#</h(\d)> \s* \n?
-                         #</h$1>\n#gix;
-        $$self{text} =~ s#</head> \s* \n?
-                         #</head>\n#gix;
-        $$self{text} =~ s#\n? <(br|p)([\s>])
-                         #\n<$1$2#gix;
-        $$self{text} =~ s#</p>\s*
-                         #</p>\n#gix;
+        $self->{text} =~ s#<(body|html)> \s* \n?
+                          #<$1>\n#gix;
+        $self->{text} =~ s#\n? <div
+                          #\n<div#gix;
+        $self->{text} =~ s#</div> \s* \n?
+                          #</div>\n#gix;
+        $self->{text} =~ s#</h(\d)> \s* \n?
+                          #</h$1>\n#gix;
+        $self->{text} =~ s#</head> \s* \n?
+                          #</head>\n#gix;
+        $self->{text} =~ s#\n? <(br|p)([\s>])
+                          #\n<$1$2#gix;
+        $self->{text} =~ s#</p>\s*
+                          #</p>\n#gix;
     }
+
     return 1;
 }
 
@@ -1481,7 +1517,7 @@ sub fix_html_filepos :method
             # Insert a whole new <a id> here
             debug(2,"DEBUG: filepos ",$pos," points to '<mbp',",
                   " creating new anchor");
-            substr($$self{text},$pos,4,'<a id="' . $pos . '"></a><mbp');
+            substr($$self{text},$pos,4,'<a id="fp' . $pos . '"></a><mbp');
         }
         elsif($atpos =~ /^<(a|p)[ >]/ix)
         {
@@ -1506,7 +1542,7 @@ sub fix_html_filepos :method
             # All other elements
             debug(2,"DEBUG: filepos ",$pos," points to '",$atpos,
                   "', creating new anchor");
-            substr($$self{text},$pos,1,'<a id="' . $pos . '"></a><');
+            substr($$self{text},$pos,1,'<a id="fp' . $pos . '"></a><');
         }
         else
         {
@@ -3024,7 +3060,7 @@ sub uncompress_dictionaryhuffman
         {
             croak($subname,"(): invalid HUFF cache offset ",$cacheoffset,
                   " found at bit position ",$bitpos,"!\n");
-            return $text;
+            #return $text;
         }
         $cacheval = $huffref->{cache}->[$cacheoffset];
 #        debug(4,"## cacheval[",$cacheoffset,"]=",$cacheval);
@@ -3034,7 +3070,7 @@ sub uncompress_dictionaryhuffman
         {
             croak($subname,"(): HUFF cache found zero codelength",
                   " at bit position ",$bitpos,"!\n");
-            return $text;
+            #return $text;
         }
 
         $nextbits = min(32,$bitoffset);
@@ -3093,7 +3129,7 @@ sub uncompress_dictionaryhuffman
         {
             croak($subname,"(): \n HUFF entry referenced invalid CDIC ",$cdic,
                   " at bit position ",$bitpos,", depth ",$depth,"!\n");
-            return $text;
+            #return $text;
         }
 
 #        debug(4,"## cdic=",$cdic);
@@ -1,10 +1,12 @@
 package EBook::Tools::PalmDoc;
 use warnings; use strict; use utf8;
-use version 0.74; our $VERSION = qv("0.4.8");
+use version 0.74; our $VERSION = qv("0.5.2");
 
 # Mixed case subs and the variable %record are inherited from Palm::PDB
 ## no critic (ProhibitAmbiguousNames)
 ## no critic (ProhibitMixedCaseSubs)
+# RequireBriefOpen seems to be way too brief to be useful
+## no critic (RequireBriefOpen)
 
 =head1 NAME
 
@@ -84,11 +86,11 @@ unless($@){ $htmlsupport = 1; }
 
 Instantiates a new Ebook::Tools::PalmDoc object.  To create a Palm
 Resource file instead of a Palm Database file, set
-C<$self->{attributes}{resource}> to be true immediately after
+C<< $self->{attributes}{resource} >> to be true immediately after
 construction.
 
 Create a new Doc object. By default, it's not a resource database. Setting
-C<$self->{attributes}{resource}> to C<1> before any manipulations will
+C<< $self->{attributes}{resource} >> to C<1> before any manipulations will
 cause it to become a resource database.
 
 =cut
@@ -282,7 +284,7 @@ sub ParseResource :method    ## no critic (Always unpack @_ first)
 =head2 C<ParseRecordBookmark($data,$currentrecord)>
 
 Parses bookmark records/resources, updating object attributes, most
-notably C<$self->{bookmarks}>.
+notably C<< $self->{bookmarks} >>.
 
 The C<$currentrecord> argument is optional, but without it the
 debugging information may be less useful.  This is called
@@ -325,7 +327,7 @@ sub ParseRecordBookmark :method   ## no critic (Always unpack @_ first)
 =head2 C<ParseRecordText($data)>
 
 Parses text records, updating object attributes, most notably
-appending text to C<$self->{text}>.
+appending text to C<< $self->{text} >>.
 
 This is called automatically by L</ParseRecord()> and
 L</ParseResource()> as needed.
@@ -853,6 +855,7 @@ sub uncompress_palmdoc
                 {
                     debug(1,"WARNING: LZ77 decompression reference is before",
                           " beginning of text!");
+                    debug(2,"  textlength=${textlength}, lz77offset=${lz77offset}");
                     return;
                 }
                 $text .= substr($text,$textpos,1);
@@ -1,7 +1,7 @@
 package EBook::Tools::Unpack;
 use warnings; use strict; use utf8;
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.9");
+use version 0.74; our $VERSION = qv("0.5.0");
 
 # Perl Critic overrides:
 ## no critic (Package variable)
@@ -60,7 +60,8 @@ use Encode;
 use Fcntl qw(SEEK_CUR SEEK_SET);
 use File::Basename qw(dirname fileparse);
 use File::Path;     # Exports 'mkpath' and 'rmtree'
-binmode(STDERR,":utf8");
+use File::Which;    # Exports 'which'
+binmode(STDERR,':encoding(UTF-8)');
 
 my $drmsupport = 0;
 eval
@@ -161,7 +162,7 @@ If specified, overrides the detected language information.
 =item * C<opffile>
 
 The name of the file in which the metadata will be stored.  If not
-specified, defaults to the value of C<dir> with C<.opf> appended.
+specified, defaults to C<content.opf>.
 
 =item * C<raw>
 
@@ -264,7 +265,7 @@ sub new   ## no critic (Always unpack @_ first)
     $self->{format} = $args{format} if($args{format});
     $self->{key} = $args{key} if($args{key});
     $self->{keyfile} = $args{keyfile} if($args{keyfile});
-    $self->{opffile} = $args{opffile} || ($self->{dir} . ".opf");
+    $self->{opffile} = $args{opffile} || "content.opf";
     $self->{author} = $args{author} if($args{author});
     $self->{title} = $args{title} if($args{title});
     $self->{datahashes} = {};
@@ -388,7 +389,7 @@ sub raw :method
 sub title :method
 {
     my $self = shift;
-    return $$self{opffile};
+    return $$self{title};
 }
 
 sub detected :method
@@ -924,7 +925,7 @@ sub unpack_mobi :method
     my $firstimagerec = 0;
 
     # Used for file output
-    my $htmlname = $self->filebase . ".html";
+    my $htmlname;
 
     my $reccount = 0; # The Record ID cannot be reliably used to identify
                       # the first record.  This increments as each
@@ -948,6 +949,12 @@ sub unpack_mobi :method
     = $mobi->{header}{mobi}{dolanguage};
 
     $self->detect_from_mobi_exth();
+    if($self->{detected}->{title}) {
+        $htmlname = clean_filename($self->{detected}->{title} . ".html");
+    }
+    else {
+        $htmlname = clean_filename($self->filebase . ".html");
+    }
 
     if($$self{raw} && !$$self{nosave})
     {
@@ -1035,7 +1042,7 @@ sub unpack_palmdoc :method
         if($$self{htmlconvert})
         {
             $outfile = $self->filebase . ".html";
-            open($fh,'>:utf8',$outfile)
+            open($fh,'>:encoding(UTF-8)',$outfile)
                 or croak($subname,"(): unable to open '",$outfile,
                          "' for writing!\n");
             print {*$fh} $pdb->html;
@@ -1090,17 +1097,23 @@ sub unpack_zip :method
     unless($self->{nosave})
     {
         my $cwd = usedir($self->{dir});
-        my $zip = Archive::Zip->new();
-        my $status = $zip->read($cwd.'/'.$self->{file});
-        if ($status != AZ_OK)
-        {
-            croak($subname,'(): error while parsing zip file "',$self->{file},'" (',$status,')!');
+
+        if (which('unzip')) {
+            my @syscmd = ( 'unzip', '-q', $cwd . '/' . $self->{file} );
+            system(@syscmd);
+            system_result($subname,$CHILD_ERROR,@syscmd);
         }
+        else {
+            my $zip = Archive::Zip->new();
+            my $status = $zip->read($cwd.'/'.$self->{file});
+            if ($status != AZ_OK) {
+                croak($subname,'(): error while parsing zip file "',$self->{file},'" (',$status,')!');
+            }
 
-        $status = $zip->extractTree(undef);
-        if ($status != AZ_OK)
-        {
-            croak($subname,'(): error while extracting zip file "',$self->{file},'" (',$status,')!');
+            $status = $zip->extractTree(undef);
+            if ($status != AZ_OK) {
+                croak($subname,'(): error while extracting zip file "',$self->{file},'" (',$status,')!');
+            }
         }
         chdir($cwd);
     }
@@ -1,9 +1,8 @@
 package EBook::Tools;
 use warnings; use strict; use utf8;
-#use 5.010; # Needed for smart-match operator
-#v5.10 feature use removed until 5.10 is standard on MacOSX and Debian
+use v5.10.1; # Needed for smart-match operator and given/when
 use English qw( -no_match_vars );
-use version 0.74; our $VERSION = qv("0.4.9");
+use version 0.74; our $VERSION = qv("0.5.4");
 
 #use warnings::unused;
 
@@ -71,10 +70,14 @@ http://search.cpan.org/perldoc?Date::Manip#TIME_ZONES
 
 =item HTML::Parser
 
+=item Lingua::EN::NameParse
+
 =item Tie::IxHash
 
 =item Time::Local
 
+=item URI::Escape
+
 =item XML::Twig
 
 =back
@@ -100,6 +103,8 @@ use base qw(Exporter);
 
 our @EXPORT_OK;
 @EXPORT_OK = qw (
+    &capitalize
+    &clean_filename
     &create_epub_container
     &create_epub_mimetype
     &debug
@@ -108,12 +113,14 @@ our @EXPORT_OK;
     &find_in_path
     &find_links
     &find_opffile
+    &hashvalue_key_self
     &hexstring
     &get_container_rootfile
     &print_memory
     &split_metadata
     &split_pre
     &strip_script
+    &system_result
     &system_tidy_xml
     &system_tidy_xhtml
     &trim
@@ -135,6 +142,9 @@ use Archive::Zip qw( :CONSTANTS :ERROR_CODES );
 use Carp;
 use Cwd qw(getcwd realpath);
 use Date::Manip;
+require EBook::Tools::BISG;
+use Encode;
+require Encode::Detect;
 use File::Basename qw(basename dirname fileparse);
 # File::MimeInfo::Magic gets *.css right, but detects all html as
 # text/html, even if it has an XML header.
@@ -147,14 +157,37 @@ use File::Path;     # Exports 'mkpath' and 'rmtree'
 use File::Temp;
 use HTML::Entities qw(decode_entities _decode_entities %entity2char);
 #use HTML::Tidy;
+use Lingua::EN::NameParse qw(case_surname);
 use Tie::IxHash;
 use Time::Local;
+use URI::Escape;
 use XML::Twig;
 
+
 =head1 CONFIGURABLE PACKAGE VARIABLES
 
 =over
 
+=item C<%bisacsubjects>
+
+A mapping of lowercase BISAC codes and text descriptions to standard
+capitalized text descriptions.  As BISG claims copyright on this and
+does not allow the lists to be redistributed, this list must be
+downloaded and cached locally via C<ebook dlbisac> before it is
+available.
+
+Running the unit tests will cause this to happen automatically.
+
+=item C<%bisactolc>
+
+An extremely incomplete mapping of lowercase BISAC codes and text
+descriptions to Library of Congress standard subjects.
+
+=item C<%booktypes>
+
+A hash mapping all-lowercase terms to a standard vocabulary to be used
+in <dc:type> elements.
+
 =item C<%dcelements12>
 
 A tied IxHash mapping an all-lowercase list of Dublin Core metadata
@@ -180,6 +213,17 @@ Order is preserved and significant -- fix_opf20 will output DC
 metadata elements in the same order as in this hash, though order for
 tags of the same name is preserved from the input file.
 
+=item C<%lcsubjects>
+
+An extremely incomplete mapping of lowercase terms to Library of
+Congress subject classifications.  This is used for automatic
+normalization of subject elements.  Every value in the hash has a
+lowercase representation of itself as a key in addition to any
+aliases.
+
+This MUST NOT contain mappings from BISAC codes or descriptors to
+Library of Congress subjects.  Use %bisactolc for that.
+
 =item C<%nonxmlentity2char>
 
 This is the %entity2char conversion map from HTML::Entities with the 5
@@ -198,12 +242,35 @@ L</fix_misc()>.
 Keys should be entered in lowercase.  The hash can also be set empty
 to prevent fix_publisher() from taking any action at all.
 
+=item C<%referencetypes>
+
+A hash mapping valid OPF 2.0 reference types to themselves, along with
+common variants to standard types.
+
 =item C<%relatorcodes>
 
 A hash mapping the MARC Relator Codes (see:
 http://www.loc.gov/marc/relators/relacode.html) to their descriptive
 names.
 
+=item C<%sexcodes>
+
+A hash normalizing subject tags for erotic fiction, as used by
+StoriesOnline, ASSTR, and Literotica, among others.
+
+Unlike the other mappings, this one is *not* lowercased, and the keys
+are case-sensitive.  The hash maps only to the canonical base code,
+but subject normalization will also add a prefix, defaulting to the
+BISAC format of 'FICTION / Erotica / '.
+
+=item C<%strangenames>
+
+=item C<%strangefileas>
+
+Hashes mapping mapping known incorrect outputs of name normalization
+to correct format.  The first handles the main name display, the
+second the file-as output.
+
 =item C<$tidycmd>
 
 The tidy executable name.  This has to be a fully qualified pathname
@@ -279,6 +346,591 @@ our $oeb12doctype =
 our $opf20package =
     '<package version="2.0" xmlns="http://www.idpf.org/2007/opf">' . "\n";
 
+my $bisg = EBook::Tools::BISG->new();
+our %bisacsubjects = $bisg->bisac();
+
+our %bisactolc = (
+    # The entire ANTXXX line is ambiguous -- it could map to either
+    # subdivisions of Antiques or Collectibles.  Work started here
+    # before this was realized is being left intact for now, but the
+    # list is incomplete for this reason.
+    'ant000000'				=> 'Antiques',
+    'ant001000'				=> 'Antiques--Americana',
+    'ant002000'				=> 'Antiques--Art',
+    'ant003000'				=> 'Autographs',
+    'ant004000'				=> 'Antiques--Baskets',
+    'ant005000'				=> 'Antiques--Books',
+    'ant006000'				=> 'Antiques--Bottles',
+    'ant010000'				=> 'Antiques--Clocks and watches',
+    'ant012000'				=> 'Antiques--Cartoons',
+    'ant015000'				=> 'Antiques--Dolls',
+    'ant016000'				=> 'Antiques--Firearms',
+    'ant017000'				=> 'Antiques--Furniture',
+    'ant018000'				=> 'Antiques--Glassware',
+    'ant021000'				=> 'Antiques--Jewelry',
+    'ant022000'				=> 'Antiques--Kitchen utensils',
+    'ant023000'				=> 'Antiques--Periodicals',
+    'ant026000'				=> 'Antiques--Musical instruments',
+    'ant031000'				=> 'Political collectibles',
+    'ant052000'				=> 'Collectibles--Popular culture',
+    'ant053000'				=> 'Antiques--Figurines',
+    'arc000000'				=> 'Architecture',
+    'arc005000'				=> 'Architecture--History',
+    'arc007000'				=> 'Interior decoration',
+    'art000000'				=> 'Art',
+    'art002000'				=> 'Airbrush art',
+    'art003000'				=> 'Calligraphy--Technique',
+    'art004000'				=> 'Cartooning',
+    'art006000'				=> 'Art collections',
+    'art008000'				=> 'Conceptual art',
+    'art009000'				=> 'Art criticism',
+    'art010000'				=> 'Drawing',
+    'art013000'				=> 'Folk art',
+    'art015010'				=> 'Art, African',
+    'art015020'				=> 'Art, American',
+    'art015030'				=> 'Art, European',
+    'art015040'				=> 'Art, Canadian',
+    'art016000'				=> 'Artists',
+    'art017000'				=> 'Mixed media (Art)',
+    'art019000'				=> 'Art, Asian',
+    'art020000'				=> 'Painting--Technique',
+    'art021000'				=> 'Pastel drawing--Technique',
+    'art023000'				=> 'Popular culture',
+    'art024000'				=> 'Prints--Technique',
+    'art026000'				=> 'Sculpture',
+    'art027000'				=> 'Arts--Study and teaching',
+    'art028000'				=> 'Art--Technique',
+    'art029000'				=> 'Watercolor painting--Technique',
+    'art033000'				=> 'Pen drawing--Technique',
+    'art034000'				=> 'Pencil drawing--Technique',
+    'art035000'				=> 'Religious art',
+    'art037000'				=> 'Politics in art',
+    'art038000'				=> 'African American art',
+    'art039000'				=> 'Asian American art',
+    'art040000'				=> 'Hispanic American art',
+    'art042000'				=> 'Art, Australian',
+    'art043000'				=> 'Art and business',
+    'art044000'				=> 'Art, Latin American',
+    'art045000'				=> 'Pottery',
+    'art046000'				=> 'Digital art',
+    'art047000'				=> 'Art--Middle East',
+    'art049000'				=> 'Art, Russian',
+    'art050000'				=> 'Art--Themes, motives',
+    'art050010'				=> 'Human beings in art',
+    'art050020'				=> 'Landscapes in art',
+    # 'ART050030' could translate to either 'Animals in art' or 'Plants in art'
+    'art050040'				=> 'Portraits',
+    'art050050'				=> 'Erotic art',
+    'art051000'				=> 'Color in art',
+    'art052000'				=> 'Human figure in art',
+    'art053000'				=> 'Sculpture--Technique',
+    'art055000'				=> 'Body art',
+    'art056000'				=> 'Art objects--Conservation and restoration',
+    'art057000'				=> 'Video art',
+    'art058000'				=> 'Street art',
+    'art060000'				=> 'Performance art',
+    'bib000000'				=> 'Bible',
+    'bio000000'				=> 'Biography',
+    'bio001000'				=> 'Artists--Biography',
+    'bio008000'				=> 'Military biography',
+    'bio009000'				=> 'Philosophers--Biography',
+    'bio011000'				=> 'Heads of state--Biography',
+    'bio022000'				=> 'Women--Biography',
+    'bio023000'				=> 'Explorers--Biography',
+    'bio024000'				=> 'Criminals--Biography',
+    'bio030000'				=> 'Environmentalists--Biography',
+    'bus000000'				=> 'Economics',
+    'bus001000'				=> 'Accounting',
+    'bus001030'				=> 'International business enterprises--Accounting',
+    'bus001050'				=> 'Accounting--Standards',
+    'bus002000'				=> 'Advertising',
+    'bus003000'				=> 'Auditing',
+    'bus004000'				=> 'Banks and banking',
+    'bus005000'				=> 'Bookkeeping',
+    'bus006000'				=> 'Budget',
+    'bus007000'				=> 'Business communication',
+    'bus007010'				=> 'Business meetings',
+    'bus008000'				=> 'Business ethics',
+    'bus009000'				=> 'Business etiquette',
+    'bus010000'				=> 'Commercial law',
+    'bus011000'				=> 'Business writing',
+    'bus012000'				=> 'Occupations',
+    'bus012010'				=> 'Internship programs',
+    'bus013000'				=> 'Commercial policy',
+    'bus014000'				=> 'Commodity exchanges',
+    'bus015000'				=> 'Consolidation and merger of corporations',
+    'bus016000'				=> 'Consumer behavior',
+    'bus017000'				=> 'Corporations--Finance',
+    'bus018000'				=> 'Customer relations',
+    'bus019000'				=> 'Decision making',
+    'bus020000'				=> 'Business--Development',
+    'bus021000'				=> 'Econometrics',
+    'bus022000'				=> 'Economic conditions',
+    'bus023000'				=> 'Economic history',
+    'bus024000'				=> 'Business education',
+    'bus025000'				=> 'Entrepreneurship',
+    'bus026000'				=> 'International trade',
+    'bus027000'				=> 'Finance',
+    'bus028000'				=> 'Foreign exchange',
+    'bus029000'				=> 'Free enterprise',
+    'bus030000'				=> 'Personnel management',
+    'bus031000'				=> 'Inflation (Finance)',
+    'bus032000'				=> 'Infrastructure (Economics)',
+    'bus033000'				=> 'Insurance',
+    'bus033010'				=> 'Automobile insurance',
+    'bus033020'				=> 'Casualty insurance',
+    'bus033040'				=> 'Health insurance',
+    'bus033050'				=> 'Liability insurance',
+    'bus033060'				=> 'Life insurance',
+    'bus033070'				=> 'Risk (Insurance)',
+    'bus033080'				=> 'Property insurance',
+    'bus034000'				=> 'Interest',
+    'bus035000'				=> 'International business enterprises',
+    'bus036000'				=> 'Investments',
+    'bus036010'				=> 'Bonds',
+    'bus036020'				=> 'Futures',
+    'bus036030'				=> 'Mutual funds',
+    'bus036040'				=> 'Options (Finance)',
+    'bus036050'				=> 'Real estate investment',
+    'bus036060'				=> 'Stocks',
+    'bus037000'				=> 'Job hunting',
+    'bus037020'				=> 'Job hunting',
+    'bus038000'				=> 'Labor',
+    'bus039000'				=> 'Macroeconomics',
+    'bus040000'				=> 'Mail-order business',
+    'bus041000'				=> 'Management',
+    'bus042000'				=> 'Management science',
+    'bus043000'				=> 'Marketing',
+    'bus043010'				=> 'Direct marketing',
+    'bus043020'				=> 'Industrial marketing',
+    'bus043030'				=> 'Export marketing',
+    'bus043040'				=> 'Multilevel marketing',
+    'bus043050'				=> 'Telemarketing',
+    'bus043060'				=> 'Marketing research',
+    'bus044000'				=> 'Microeconomics',
+    'bus045000'				=> 'Monetary policy',
+    'bus046000'				=> 'Achievement motivation',
+    'bus047000'				=> 'Negotiation',
+    'bus048000'				=> 'New business enterprises',
+    'bus049000'				=> 'Operations research',
+    'bus050000'				=> 'Finance, Personal',
+    'bus050010'				=> 'Budgets, Personal',
+    'bus050020'				=> 'Investments',
+    'bus050040'				=> 'Retirement--Planning',
+    'bus050050'				=> 'Income tax',
+    'bus051000'				=> 'Finance, Public',
+    'bus052000'				=> 'Public relations',
+    'bus053000'				=> 'Quality control',
+    'bus054000'				=> 'Real property',
+    # 'bus054010' could be either House Buying or House Selling
+    'bus054020'				=> 'Commercial real estate',
+    'bus054030'				=> 'Mortgages',
+    'bus055000'				=> 'Business--Reference books',
+    'bus056030'				=> 'Résumés (Employment)',
+    'bus057000'				=> 'Retail trade',
+    # 'bus058000' could be either Sales or Selling
+    'bus058010'				=> 'Sales management',
+    'bus059000'				=> 'Business--Training',
+    'bus060000'				=> 'Small business',
+    # 'bus061000' could be either 'Commercial statistics' or 'Economic statistics'
+    'bus062000'				=> 'Structural adjustment (Economic policy)',
+    'bus063000'				=> 'Strategic planning',
+    'bus064000'				=> 'Taxation',
+    'bus064010'				=> 'Corporations--Taxation',
+    'bus064020'				=> 'International business enterprises--Taxation',
+    'bus064030'				=> 'Small business--Taxation',
+    'bus065000'				=> 'Total quality management',
+    'bus066000'				=> 'Business--Training',
+    'bus067000'				=> 'Regional economics',
+    'bus068000'				=> 'Economic development',
+    'bus069000'				=> 'Economics',
+    'bus069010'				=> 'Comparative economics',
+    'bus069020'				=> 'Competition, International',
+    'bus069030'				=> 'Economics',
+    'bus070000'				=> 'Industries',
+    'bus070010'				=> 'Agricultural industries',
+    'bus070020'				=> 'Automobile industry and trade',
+    'bus070030'				=> 'Computer industry',
+    'bus070040'				=> 'Energy industries',
+    'bus070050'				=> 'Manufacturing industries',
+    'bus070060'				=> 'Communication and traffic',
+    'bus070070'				=> 'Parks--Management',
+    'bus070080'				=> 'Service industries',
+    'bus070090'				=> 'Textile industry',
+    'bus070100'				=> 'Transportation',
+    'bus070110'				=> 'Entertainment industry',
+    'bus070120'				=> 'Food industry and trade',
+    'bus070130'				=> 'Biotechnology industries',
+    'bus070140'				=> 'Financial services industry',
+    'bus070150'				=> 'Natural resources',
+    'bus071000'				=> 'Leadership',
+    'bus072000'				=> 'Sustainable development',
+    'bus073000'				=> 'Commerce',
+    'bus074000'				=> 'Nonprofit organizations',
+    # 'bus075000' is 'Consulting' which has no exact LC match
+    'bus076000'				=> 'Purchasing',
+    'bus077000'				=> 'Commerce--History',
+    'bus078000'				=> 'Distribution (Economic theory)',
+    'bus079000'				=> 'Industrial policy',
+    'bus080000'				=> 'Home-based businesses',
+    'bus081000'				=> 'Tourism',
+    'bus082000'				=> 'Industrial management',
+    'bus083000'				=> 'Information resources management',
+    'bus084000'				=> 'Office practice--Automation',
+    'bus085000'				=> 'Organizational behavior',
+    'bus086000'				=> 'Business forecasting',
+    'bus087000'				=> 'Production management',
+    'bus088000'				=> 'Time management',
+    'bus089000'				=> 'Secretaries--Training',
+    'bus090000'				=> 'Electronic commerce',
+    'bus090010'				=> 'Internet advertising',
+    'bus090020'				=> 'Internet banking',
+    'bus090030'				=> 'Electronic trading of securities',
+    'bus090040'				=> 'Internet auctions',
+    'bus091000'				=> 'Business mathematics',
+    'bus092000'				=> 'Economic development',
+    'bus093000'				=> 'Facility management',
+    'bus094000'				=> 'Business enterprises--Environmental aspects',
+    'bus095000'				=> 'Office equipment and supplies',
+    'bus096000'				=> 'Office management',
+    'bus097000'				=> 'Work environment',
+    'bus098000'				=> 'Intellectual capital',
+    'bus099000'				=> 'Environmental economics',
+    'bus100000'				=> 'Museum techniques',
+    'bus101000'				=> 'Project management',
+    'bus102000'				=> 'Contracting out',
+    'bus103000'				=> 'Organizational change',
+    'bus104000'				=> 'Corporate governance',
+    'bus105000'				=> 'Franchises (Retail trade)',
+    'bus106000'				=> 'Executive coaching',
+    'bus107000'				=> 'Success',
+    'bus108000'				=> 'Research and development projects',
+    'bus109000'				=> 'Businesswomen',
+    'bus110000'				=> 'Conflict management',
+    'cgn000000'				=> 'Comic books, strips, etc',
+    'cgn004080'				=> 'Superheroes',
+    'ckb000000'				=> 'Cooking',
+    'ckb001000'				=> 'Cooking, African',
+    'ckb002000'				=> 'Cooking, American',
+    'ckb002010'				=> 'Cooking, American--California style',
+    'ckb002020'				=> 'Cooking, American--Middle Atlantic States',
+    'ckb002030'				=> 'Cooking, American--Midwestern style',
+    'ckb002040'				=> 'Cooking, American--New England style',
+    'ckb002050'				=> 'Cooking, American--Northwestern States',
+    'ckb002060'				=> 'Cooking, American--Southern style',
+    'ckb002070'				=> 'Cooking, American--Southwestern style',
+    'ckb002080'				=> 'Cooking, American--Western style',
+    'ckb003000'				=> 'Appetizers',
+    'ckb004000'				=> 'Baking',
+    'ckb005000'				=> 'Barbecuing',
+    'ckb006000'				=> 'Bartending',
+    'ckb007000'				=> 'Cooking (Beer)',
+    'ckb008000'				=> 'Non-alcoholic beverages',
+    'ckb009000'				=> 'Cooking (Bread)',
+    'ckb010000'				=> 'Breakfasts',
+    # 'ckb011000' could be 'Cooking, English', 'Cooking, Scottish', or 'Cooking, Welsh'
+    'ckb012000'				=> 'Brunches',
+    # 'ckb013000' could be either 'Cooking, Cajun' or 'Cooking, Creole'
+    'ckb014000'				=> 'Cake',
+    'ckb015000'				=> 'Canning and preserving',
+    # 'ckb016000' could be either 'Cooking, Caribbean' or 'Cooking, West Indian'
+    'ckb017000'				=> 'Cooking, Chinese',
+    'ckb018000'				=> 'Cooking (Chocolate)',
+    # 'ckb019000' could be either 'Cooking (Coffee)' or 'Cooking (Tea)'
+    'ckb020000'				=> 'Cooking for one',
+    'ckb021000'				=> 'Cookies',
+    'ckb024000'				=> 'Desserts',
+    'ckb026000'				=> 'Weight loss',
+    'ckb029000'				=> 'Entertaining',
+    'ckb033000'				=> 'Garnishes (Cooking)',
+    'ckb034000'				=> 'Cooking, French',
+    'ckb036000'				=> 'Cooking, German',
+    'ckb038000'				=> 'Cooking, Greek',
+    # 'ckb040000' could be 'Cooking (Herbs)', 'Cooking (Spices)', or 'Condiments'
+    'ckb041000'				=> 'Cooking--History',
+    'ckb042000'				=> 'Holiday cooking',
+    'ckb050000'				=> 'Low-cholesterol diet',
+    'ckb051000'				=> 'Low-fat diet',
+    'ckb057000'				=> 'Microwave cooking',
+    'ckb060000'				=> 'Outdoor cooking',
+    'ckb062000'				=> 'Pastry',
+    'ckb063000'				=> 'Pies',
+    'ckb064000'				=> 'Pizza',
+    'ckb069000'				=> 'Quantity cooking',
+    'ckb070000'				=> 'Quick and easy cooking',
+    'ckb073000'				=> 'Salads',
+    # 'ckb079000' could map to either 'Soups' or 'Stews'
+    'ckb088000'				=> 'Cooking (Liquors)',
+    'ckb089000'				=> 'Wok cooking',
+    'ckb090000'				=> 'Cooking, Asian',
+    'ckb091000'				=> 'Cooking, Canadian',
+    'ckb092000'				=> 'Cooking, European',
+    'ckb095000'				=> 'Confectionery',
+    'ckb099000'				=> 'Cooking, Latin American',
+    'ckb100000'				=> 'Beverages',
+    'ckb101000'				=> 'Entrées (Cooking)',
+    'ckb102000'				=> 'Sauces',
+    'ckb103000'				=> 'Cancer--Nutritional aspects',
+    'ckb104000'				=> 'Heart--Diseases--Nutritional aspects',
+    'ckb106000'				=> 'Food allergy',
+    'ckb107000'				=> 'Baby foods',
+    'ckb108000'				=> 'Low-carbohydrate diet',
+    'ckb110000'				=> 'Raw foods',
+    'ckb111000'				=> 'Gluten-free foods',
+    'ckb112000'				=> 'Casserole cooking',
+    'com000000'				=> 'Computers',
+    'com001000'				=> 'Computer peripherals',
+    'com003000'				=> 'Application software',
+    'com004000'				=> 'Artificial intelligence',
+    "com006000"				=> "Computers--Buyers' guides",
+    'com007000'				=> 'CAD/CAM systems',
+    'com008000'				=> 'Calculators',
+    'com009000'				=> 'Optical disks',
+    'com010000'				=> 'Compilers (Computer programs)',
+    'com011000'				=> 'Computer architecture',
+    'com012000'				=> 'Computer graphics',
+    'com012040'				=> 'Computer games--Programming',
+    'com012050'				=> 'Image processing',
+    'com013000'				=> 'Computer literacy',
+    'com014000'				=> 'Computer science',
+    'com015000'				=> 'Malware (Computer software)',
+    # 'com016000' could be either 'Computer vision' or 'Pattern recognition systems'
+    'com018000'				=> 'Data processing',
+    'com020000'				=> 'Data transmission systems',
+    'com020010'				=> 'Electronic data interchange',
+    'com020050'				=> 'Broadband communication systems',
+    'com020060'				=> 'Modems',
+    'com020090'				=> 'Wireless communication systems',
+    'com021000'				=> 'Databases',
+    'com021030'				=> 'Data mining',
+    'com021040'				=> 'Data warehousing',
+    'com023000'				=> 'Computer software--Education',
+    'com024000'				=> 'Computer games',
+    'com025000'				=> 'Expert systems (Computer science)',
+    'com027000'				=> 'Finance, Personal--Software',
+    'com031000'				=> 'Information theory',
+    'com032000'				=> 'Information technology',
+    'com034000'				=> 'Interactive computer systems',
+    'com035000'				=> 'Keyboarding',
+    'com036000'				=> 'Logic design',
+    'com037000'				=> 'Machine theory',
+    'com039000'				=> 'Management information systems',
+    'com040000'				=> 'Memory management (Computer science)',
+    'com041000'				=> 'Microprocessors',
+    'com042000'				=> 'Computational linguistics',
+    'com043000'				=> 'Computer networks',
+    'com043020'				=> 'Local area networks (Computer networks)',
+    'com043040'				=> 'Computer network protocols',
+    'com043050'				=> 'Computer networks--Security measures',
+    'com044000'				=> 'Neural networks (Computer science)',
+    'com046000'				=> 'Operating systems (Computers)',
+    'com046090'				=> 'Virtual computer systems',
+    'com047000'				=> 'Optical data processing',
+    'com048000'				=> 'Electronic data processing--Distributed processing',
+    'com049000'				=> 'Computer peripherals',
+    'com050000'				=> 'Microcomputers',
+    'com050020'				=> 'Macintosh-compatible computers',
+    'com051000'				=> 'Computer programming',
+    'com051010'				=> 'Programming languages (Electronic computers)',
+    'com051020'				=> 'Ada (Computer program language)',
+    'com051030'				=> 'C (Computer program language)',
+    'com051040'				=> 'Assembly languages (Electronic computers)',
+    "com051050"				=> 'BASIC (Computer program language)',
+    'com051060'				=> 'C (Computer program language)',
+    'com051070'				=> 'C++ (Computer program language)',
+    'com051080'				=> 'COBOL (Computer program language)',
+    'com051090'				=> 'FORTRAN (Computer program language)',
+    'com051100'				=> 'LISP (Computer program language)',
+    'com051110'				=> 'C (Computer program language)',
+    'com051120'				=> 'Modula-2 (Computer program language)',
+    'com051130'				=> 'Pascal (Computer program language)',
+    'com051140'				=> 'Prolog (Computer program language)',
+    'com051150'				=> 'C (Computer program language)',
+    'com051160'				=> 'Smalltalk (Computer program language)',
+    'com051170'				=> 'SQL (Computer program language)',
+    'com051180'				=> 'C (Computer program language)',
+    'com051190'				=> 'Pascal (Computer program language)',
+    'com051210'				=> 'Object-oriented programming (Computer science)',
+    'com051220'				=> 'Parallel programming (Computer science)',
+    # 'com051230' could be either 'Computer software--Development' or 'Software engineering'
+    'com051240'				=> 'System analysis',
+    'com051260'				=> 'JavaScript (Computer program language)',
+    'com051270'				=> 'HTML (Document markup language)',
+    'com051280'				=> 'Java (Computer program language)',
+    'com051290'				=> 'RPG (Computer program language)',
+    'com051300'				=> 'Computer programming--Algorithms',
+    'com051310'				=> 'C# (Computer program language)',
+    'com051320'				=> 'XML (Document markup language)',
+    'com051330'				=> 'Computer software--Quality assurance',
+    'com051340'				=> 'Computer network protocols',
+    'com051350'				=> 'Perl (Computer program language)',
+    'com051360'				=> 'Python (Computer program language)',
+    'com051400'				=> 'PHP (Computer program language)',
+    'com051410'				=> 'Ruby (Computer program language)',
+    'com051420'				=> 'VBScript (Computer program language)',
+    'com051430'				=> 'Software engineering--Project management',
+    'com051450'				=> 'UML (Computer science)',
+    'com052000'				=> 'Computers--Reference',
+    'com053000'				=> 'Computer security',
+    'com054000'				=> 'Electronic spreadsheets--Software',
+    'com054010'				=> 'Electronic spreadsheets--Software',
+    'com054020'				=> 'Electronic spreadsheets--Software',
+    'com055000'				=> 'Computer technicians--Certification--Study guides',
+    'com057000'				=> 'Virtual reality',
+    'com058000'				=> 'Word processing--Software',
+    'com058010'				=> 'Word processing--Software',
+    'com058020'				=> 'Word processing--Software',
+    'com059000'				=> 'Computer engineering',
+    'com060000'				=> 'Internet',
+    # 'com060030' could be either 'Extranets (Computer networks)' or
+    # 'Intranets (Computer networks)'
+    'com060010'				=> 'Browsers (Computer programs)',
+    'com060040'				=> 'Internet--Safety measures',
+    'com060070'				=> 'Web sites--Directories',
+    'com060080'				=> 'World Wide Web',
+    'com060090'				=> 'Application software--Internet--Development',
+    'com060100'				=> 'Blogs',
+    'com060110'				=> 'Webcasting',
+    'com060120'				=> 'Web search engines',
+    'com060130'				=> 'Web sites--Design',
+    'com060140'				=> 'Online social networks',
+    'com060150'				=> 'User-generated content',
+    'com060160'				=> 'Web site development',
+    'com060180'				=> 'Web services',
+    'com063000'				=> 'Document imaging systems',
+    'com064000'				=> 'Electronic commerce',
+    'com065000'				=> 'Electronic publishing',
+    'com067000'				=> 'Computers',
+    'com069000'				=> 'Online information services',
+    'com069010'				=> 'Online information services',
+    'com070000'				=> 'User interfaces (Computer systems)',
+    'com071000'				=> 'Computer animation',
+    'com072000'				=> 'Computer simulation',
+    'com073000'				=> 'Speech processing systems',
+    'com074000'				=> 'Mobile computing',
+    'com076000'				=> 'Computers',
+    'com077000'				=> 'Computer software--Mathematics',
+    'com078000'				=> 'Presentation graphics software',
+    'com079000'				=> 'Computers--Social aspects',
+    'com079010'				=> 'Human-computer interaction',
+    'com080000'				=> 'Computers--History',
+    'com081000'				=> 'Project management--Software',
+    'com082000'				=> 'Bioinformatics',
+    'com083000'				=> 'Cryptography',
+    'com084000'				=> 'Application software',
+    'com084010'				=> 'Databases',
+    'com084020'				=> 'Electronic mail systems',
+    'com085000'				=> 'Technical writing',
+    'com087000'				=> 'Digital media',
+    'com088000'				=> 'Computers--Administration',
+    'com090000'				=> 'Tablet computers',
+    'computers/programming languages/javascript' => 'JavaScript (Computer program language)',
+    'computers/programming languages'	=> 'Programming languages (Electronic computers)',
+    'computers/reference'		=> 'Computers--Reference',
+    'juv000000'				=> 'Fiction, Juvenile',
+    'fic000000'				=> 'Fiction',
+    'fic009000'				=> 'Fantasy fiction',
+    'fic014000'				=> 'Historical fiction',
+    'fic027000'				=> 'Love stories',
+    'occ000000'				=> 'Superstition',
+    'occ003000'				=> 'Channeling (Spiritualism)',
+    'occ004000'				=> 'Crystals',
+    'occ005000'				=> 'Divination',
+    'occ006000'				=> 'Dreams',
+    'occ007000'				=> 'Extrasensory perception',
+    'occ008000'				=> 'Fortune-telling',
+    'occ009000'				=> 'Horoscopes',
+    'occ010000'				=> 'Meditation',
+    'occ011000'				=> 'Mental healing',
+    'occ011010'				=> 'Energy medicine',
+    'occ011020'				=> 'Spiritual healing',
+    'occ014000'				=> 'New Thought',
+    'occ015000'				=> 'Numerology',
+    'occ016000'				=> 'Occultism',
+    'occ017000'				=> 'Palmistry',
+    'occ018000'				=> 'Parapsychology',
+    'occ019000'				=> 'Inspiration--Religious aspects',
+    'occ020000'				=> 'Prophecy',
+    'occ022000'				=> 'Immortality',
+    'occ023000'				=> 'Supernatural',
+    'occ024000'				=> 'Tarot',
+    'occ025000'				=> 'Unidentified flying objects',
+    'occ026000'				=> 'Witchcraft',
+    'occ027000'				=> 'Spiritualism',
+    'occ028000'				=> 'Magic',
+    'occ031000'				=> 'Controversial literature',
+    'occ032000'				=> 'Guides (Spiritualism)',
+    'occ034000'				=> 'Near-death experiences',
+    'occ035000'				=> 'Astral projection',
+    'occ036000'				=> 'Spirituality',
+    'occ036030'				=> 'Shamanism',
+    'occ036050'				=> 'Goddess religion',
+    'occ037000'				=> 'Feng shui',
+    'occ039000'				=> 'Hallucinogenic drugs',
+    'occ040000'				=> 'Hermetism',
+    'occ041000'				=> 'Sex--Religious aspects',
+   );
+
+our %booktypes = (
+    # Fiction types, by length (definitions are not standardized)
+    'micro fiction'	=> 'Micro Fiction',	# Below 100 words
+    'micro-fiction'	=> 'Micro Fiction',
+    'drabble'		=> 'Drabble',		# 100 words, exactly
+    'flash fiction'	=> 'Flash Fiction',	# Below 1000 words
+    'short short'	=> 'Flash Fiction',
+    'short short story'	=> 'Flash Fiction',
+    'short story'	=> 'Short Story',	# Generally 1k-10k words
+    'short'		=> 'Short Story',
+    'novelette'		=> 'Novelette',		# Generally 7.5k-15k words
+    'novella'		=> 'Novella',		# Generally 12k-40k words
+    'novel'		=> 'Novel',		# Over 50k words
+    'anthology'		=> 'Anthology',		# Multiple novellas or shorter
+    'omnibus'		=> 'Omnibus',		# Multiple novels
+    'complete works'	=> 'Complete Works',	# Complete collection of all works by an author
+    'opera omnia'	=> 'Complete Works',
+    # Age categories
+    'picture book'	=> 'Picture Book',	# For pre-readers or children ages 0-5
+    'early reader'	=> 'Early Reader',	# For ages 5-7
+    'chapter book'	=> 'Chapter Book',	# For ages 7-12
+    'young-adult novel'	=> 'Young-Adult Novel',	# Subcategory of Novel aimed at ages 12-18
+    'ya novel'		=> 'Young-Adult Novel',
+    # Non-fiction types
+    'academic paper'	=> 'Academic Paper',	# Creditable, peer-reviewed sources
+    'almanac'		=> 'Almanac',
+    'autobiography'	=> 'Autobiography',
+    'biography'		=> 'Biography',
+    'book report'	=> 'Book Report',
+    'creative nonfiction' => 'Creative Nonfiction', # Flexible imaginings of actual events
+    'design document'	=> 'Design Document',
+    'diary'		=> 'Diary',
+    'dictionary',	=> 'Dictionary',
+    'encyclopædia'	=> 'Encyclopedia',
+    'encyclopaedia'	=> 'Encyclopedia',
+    'encyclopedia'	=> 'Encyclopedia',
+    'essay'		=> 'Essay',
+    'essay collection'	=> 'Essay Collection',
+    'journal'		=> 'Journal',		# Collection of papers, academic or technical, with or without review
+    'journalism'	=> 'Journalism',
+    'letter'		=> 'Letters',		# Correspondence, singular or collective
+    'letters'		=> 'Letters',
+    'memoir'		=> 'Memoir',
+    'popular science'	=> 'Popular Science',	# Interpretation of science for a general audience
+    'reference'		=> 'Reference',		# Includes blueprints, guides, manuals, diagrams,
+						# but see also 'Technical Writing' below
+    'technical writing'	=> 'Technical Writing',	# Includes 'how-to' books even on nontechnical topics
+    'technical'		=> 'Technical Writing',
+    'thesaurus'		=> 'Thesaurus',
+    'travelogue'	=> 'Travelogue',
+    # Other types
+    'art'		=> 'Art',		# Collection of artistic images
+    'comic book'	=> 'Comic Book',
+    'graphic novel'	=> 'Graphic Novel',
+    'photography'	=> 'Photography',
+    'poetry'		=> 'Poetry',		# One or more structured poems
+    'prose poetry'	=> 'Prose',		# One or more unstructured prose works
+    'prose'		=> 'Prose',
+);
+
 our %dcelements12;
 tie %dcelements12, 'Tie::IxHash', (
     "dc:identifier"  => "dc:Identifier",
@@ -319,6 +971,2954 @@ tie %dcelements20, 'Tie::IxHash', (
     "dc:copyrights"  => "dc:rights"
     );
 
+our %lcsubjects = (
+    'abolition of capital punishment'	=> 'Capital punishment',
+    'abolition of slavery'		=> 'Slavery',
+    'abolition'				=> 'Slavery',
+    'absent treatment'			=> 'Mental healing',
+    'abstract automata'			=> 'Machine theory',
+    'abstract machines'			=> 'Machine theory',
+    'absurd (philosophy)'		=> 'Absurd (Philosophy)',
+    'absurdist (philosophy)'		=> 'Absurd (Philosophy)',
+    'absurdist'				=> 'Absurd (Philosophy)',
+    'abuse of children'			=> 'Child abuse',
+    'accountancy'			=> 'Accounting',
+    'accounting'			=> 'Accounting',
+    'accounting standards'		=> 'Accounting--Standards',
+    'accounting--standards'		=> 'Accounting--Standards',
+    'achievement motivation'		=> 'Achievement motivation',
+    'aching, tiffany (fictitious character)' => 'Aching, Tiffany (Fictitious character)',
+    'aching, tiffany'			=> 'Aching, Tiffany (Fictitious character)',
+    'acquisition of corporations'	=> 'Consolidation and merger of corporations',
+    'acquisitions and mergers'		=> 'Consolidation and merger of corporations',
+    'action research'			=> 'Action research',
+    'acts of terrorism'			=> 'Terrorism',
+    'ada (computer program language)'	=> 'Ada (Computer program language)',
+    'ada'				=> 'Ada (Computer program language)',
+    'adding machines'			=> 'Calculators',
+    'adding-machines'			=> 'Calculators',
+    'adiposity'				=> 'Obesity',
+    'administration'			=> 'Management',
+    'administrative communication'	=> 'Business communication',
+    'ads'				=> 'Advertising',
+    'adventure'				=> 'Adventure stories',
+    'adventure stories'			=> 'Adventure stories',
+    'advertisements'			=> 'Advertising',
+    'advertising'			=> 'Advertising',
+    'african art'			=> 'Art, African',
+    'african american art'		=> 'African American art',
+    'african cooking'			=> 'Cooking, African',
+    'afro-american art'			=> 'African American art',
+    'afterlife'				=> 'Immortality',
+    'agents, search'			=> 'Search engines',
+    'agoraphobia'			=> 'Agoraphobia',
+    'agribusiness'			=> 'Agricultural industries',
+    'agricultural industries'		=> 'Agricultural industries',
+    'aias (greek mythology)'		=> 'Ajax (Greek mythology)',
+    'ajax (greek mythology)'		=> 'Ajax (Greek mythology)',
+    'ajax (legendary character)'	=> 'Ajax (Greek mythology)',
+    'ajax (web site development technology)' => 'Ajax (Web site development technology)',
+    'ajax the greater (greek mythology)' => 'Ajax (Greek mythology)',
+    'ajax the greater'			=> 'Ajax (Greek mythology)',
+    'algorism'				=> 'Algorithms',
+    'algorithmic knowledge discovery'	=> 'Data mining',
+    'algorithms'			=> 'Algorithms',
+    'allocation of time'		=> 'Time management',
+    'amalgamation of corporations'	=> 'Consolidation and merger of corporations',
+    'american cooking'			=> 'Cooking, American',
+    'americana'				=> 'Americana',
+    'androids'				=> 'Androids',
+    'animation, computer'		=> 'Computer animation',
+    'angelology'			=> 'Angels',
+    'angels'				=> 'Angels',
+    'angleterre'			=> 'England',
+    'anglii͡a'				=> 'England',
+    'animals in art'			=> 'Animals in art',
+    'antique and classic cars'		=> 'Antique and classic cars',
+    'antique cars'			=> 'Antique and classic cars',
+    'antiques'				=> 'Antiques',
+    'antiques--americana'		=> 'Antiques--Americana',
+    'antiques--art'			=> 'Antiques--Art',
+    'antiques--baskets'			=> 'Antiques--Baskets',
+    'antiques--books'			=> 'Antiques--Books',
+    'antiques--bottles'			=> 'Antiques--Bottles',
+    'antiques--buttons'			=> 'Antiques--Buttons',
+    'antiques--cartoons'		=> 'Antiques--Cartoons',
+    'antiques--clocks and watches'	=> 'Antiques--Clocks and watches',
+    'antiques--coins'			=> 'Antiques--Coins',
+    'antiques--dolls'			=> 'Antiques--Dolls',
+    'antiques--figurines'		=> 'Antiques--Figurines',
+    'antiques--firearms'		=> 'Antiques--Firearms',
+    'antiques--furniture'		=> 'Antiques--Furniture',
+    'antiques--glassware'		=> 'Antiques--Glassware',
+    'antiques--jewelry'			=> 'Antiques--Jewelry',
+    'antiques--kitchen utensils'	=> 'Antiques--Kitchen utensils',
+    'antiques--musical instruments'	=> 'Antiques--Musical instruments',
+    'antiques--periodicals'		=> 'Antiques--Periodicals',
+    'antislavery'			=> 'Slavery',
+    'appearance bias'			=> 'Physical-appearance-based bias',
+    'appearance discrimination'		=> 'Physical-appearance-based bias',
+    'appearance-based bias'		=> 'Physical-appearance-based bias',
+    'appearance-based discrimination'	=> 'Physical-appearance-based bias',
+    'ai'				=> 'Artificial intelligence',
+    'airbrush art'			=> 'Airbrush art',
+    'airbrush art--technique'		=> 'Airbrush art',
+    'ais'				=> 'Artificial intelligence',
+    'alimentation',			=> 'Nutrition',
+    'alsatian cooking'			=> 'Cooking, French--Alsatian style',
+    'alternate histories'		=> 'Alternative histories (Fiction)',
+    'alternate histories (fiction)'	=> 'Alternative histories (Fiction)',
+    'alternate history'			=> 'Alternative histories (Fiction)',
+    'alternative histories'		=> 'Alternative histories (Fiction)',
+    'alternative histories (fiction)'	=> 'Alternative histories (Fiction)',
+    'alternative history'		=> 'Alternative histories (Fiction)',
+    'american art'			=> 'Art, American',
+    'american science fiction and fantasy' => 'Speculative fiction',
+    'appetizers'			=> 'Appetizers',
+    'application computer programs'	=> 'Application software',
+    'application computer software'	=> 'Application software',
+    'application software'		=> 'Application software',
+    'application software--development'	=> 'Application software--Development',
+    'application software--internet'	=> 'Application software--Internet',
+    'application software--internet--development' => 'Application software--Internet--Development',
+    'applications software'		=> 'Application software',
+    'apps (computer software)'		=> 'Application software',
+    'apps'				=> 'Application software',
+    'ararat, mount (turkey)'		=> 'Ararat, Mount (Turkey)',
+    'architecture'			=> 'Architecture',
+    'architecture, computer'		=> 'Computer architecture',
+    'architecture--history'		=> 'Architecture--History',
+    'arithmetic'			=> 'Mathematics',
+    'art'				=> 'Art',
+    'art and business'			=> 'Art and business',
+    'art collections'			=> 'Art collections',
+    'art criticism'			=> 'Art criticism',
+    'art objects'			=> 'Art objects',
+    'art objects--conservation and restoration'
+      => 'Art objects--Conservation and restoration',
+    'art technique'			=> 'Art--Technique',
+    'art, african'			=> 'Art, African',
+    'art, african american'		=> 'African American art',
+    'art, american'			=> 'Art, American',
+    'art, asian'			=> 'Art, Asian',
+    'art, asian american'		=> 'Asian American art',
+    'art, asiatic'			=> 'Art, Asian',
+    'art, australian'			=> 'Art, Australian',
+    'art, canadian'			=> 'Art, Canadian',
+    'art, caribbean'			=> 'Art, Caribbean',
+    'art, conceptual'			=> 'Conceptual art',
+    'art, digital'			=> 'Digital art',
+    'art, erotic'			=> 'Erotic art',
+    'art, european'			=> 'Art, European',
+    'art, folk'				=> 'Folk art',
+    'art, hispanic american'		=> 'Hispanic American art',
+    'art, latin american'		=> 'Art, Latin American',
+    'art, middle east'			=> 'Art--Middle East',
+    'art, middle eastern'		=> 'Art--Middle East',
+    'art, modern'			=> 'Art, Modern',
+    'art, modern--europe'		=> 'Art, Modern--Europe',
+    'art, oriental'			=> 'Art, Asian',
+    'art, performance'			=> 'Performance art',
+    'art, russian'			=> 'Art, Russian',
+    'art, spanish american'		=> 'Art, Latin American',
+    'art, street'			=> 'Street art',
+    'art, video'			=> 'Video art',
+    'art, wall'				=> 'Street art',
+    'art--criticism'			=> 'Art criticism',
+    'art--middle east'			=> 'Art--Middle East',
+    'art--subjects'			=> 'Art--Themes, motives',
+    'art--technique'			=> 'Art--Technique',
+    'art--themes, motives'		=> 'Art--Themes, motives',
+    'artificial intelligence'		=> 'Artificial intelligence',
+    'artificial neural networks'	=> 'Neural networks (Computer science)',
+    'artificial thinking'		=> 'Artificial intelligence',
+    'artistic technique'		=> 'Art--Technique',
+    'artists'				=> 'Artists',
+    'artists--biography'		=> 'Artists--Biography',
+    'arts--criticism'			=> 'Art criticism',
+    'arts--study and teaching'		=> 'Arts--Study and teaching',
+    'asian american art'		=> 'Asian American art',
+    'asian cookery'			=> 'Cooking, Asian',
+    'asian cooking'			=> 'Cooking, Asian',
+    'asiatic art'			=> 'Art, Asian',
+    'assembler language (computer program language)'	=> 'Assembly languages (Electronic computers)',
+    'assembler language (electronic computers)'	=> 'Assembly languages (Electronic computers)',
+    'assembler language'		=> 'Assembly languages (Electronic computers)',
+    'assembler languages (electronic computers)' => 'Assembly languages (Electronic computers)',
+    'assembler languages'		=> 'Assembly languages (Electronic computers)',
+    'assembly language (electronic computers)'	=> 'Assembly languages (Electronic computers)',
+    'assembly language'			=> 'Assembly languages (Electronic computers)',
+    'assembly languages (electronic computers)'	=> 'Assembly languages (Electronic computers)',
+    'assembly languages'		=> 'Assembly languages (Electronic computers)',
+    'assurance (insurance)'		=> 'Insurance',
+    'astral projection'			=> 'Astral projection',
+    'astral travel'			=> 'Astral projection',
+    'astrology'				=> 'Astrology',
+    'astrology, chinese'		=> 'Astrology, Chinese',
+    'asynchronous javascript and xml'	=> 'Ajax (Web site development technology)',
+    'athletic competitions'		=> 'Sports',
+    'athletics'				=> 'Sports',
+    'attacks, terrorist'		=> 'Terrorism',
+    'auditing'				=> 'Auditing',
+    'audits'				=> 'Auditing',
+    'augury'				=> 'Divination',
+    'austen, jane, 1775-1817'		=> 'Austen, Jane, 1775-1817',
+    'austen, jane'			=> 'Austen, Jane, 1775-1817',
+    'australian art'			=> 'Art, Australian',
+    'authoring (authorship)'		=> 'Authorship',
+    'authoring'				=> 'Authorship',
+    'authorship'			=> 'Authorship',
+    'autobiography'			=> 'Biography',
+    'autographs'			=> 'Autographs',
+    'automatic computers'		=> 'Computers',
+    'automatic data processing equipment' => 'Computers',
+    'automatic data processors'		=> 'Computers',
+    'automatic drafting'		=> 'Computer graphics',
+    'automatic language processing'	=> 'Computational linguistics',
+    'automobile industry'		=> 'Automobile industry and trade',
+    'automobile industry and trade'	=> 'Automobile industry and trade',
+    'automobile collision insurance'	=> 'Automobile insurance',
+    'automobile insurance'		=> 'Automobile insurance',
+    'automobile liability insurance'	=> 'Automobile insurance',
+    'automobiles'			=> 'Automobiles',
+    'automotive industry'		=> 'Automobile industry and trade',
+    'auvergnat cooking'			=> 'Cooking, French--Auvergne style',
+    'auvergne cooking'			=> 'Cooking, French--Auvergne style',
+    'b-to-b marketing'			=> 'Industrial marketing',
+    'b2b marketing'			=> 'Industrial marketing',
+    'babies'				=> 'Infants',
+    'baby foods'			=> 'Baby foods',
+    'bagdad (iraq)'			=> 'Baghdad (Iraq)',
+    'bagdad'				=> 'Baghdad (Iraq)',
+    'bagddad (iraq)'			=> 'Baghdad (Iraq)',
+    'bagddad'				=> 'Baghdad (Iraq)',
+    'baghdad (iraq)'			=> 'Baghdad (Iraq)',
+    'baghdad'				=> 'Baghdad (Iraq)',
+    'baking bread'			=> 'Cooking (Bread)',
+    'baking'				=> 'Baking',
+    'bank robberies'			=> 'Bank robberies',
+    'banking industry'			=> 'Banks and banking',
+    'banking'				=> 'Banks and banking',
+    'banks and banking'			=> 'Banks and banking',
+    'barbecue cookery'			=> 'Barbecuing',
+    'barbecue cooking'			=> 'Barbecuing',
+    'barbecuing'			=> 'Barbecuing',
+    'barbeque cookery'			=> 'Barbecuing',
+    'barbeque cooking'			=> 'Barbecuing',
+    'barbequing'			=> 'Barbecuing',
+    'bargaining'			=> 'Negotiation',
+    'bartending'			=> 'Bartending',
+    'basic (computer program language)'	=> 'BASIC (Computer program language)',
+    'basic rights'			=> 'Civil rights',
+    'baskets'				=> 'Baskets',
+    'bavarian cooking'			=> 'Cooking, German--Bavarian style',
+    'bbq cooking'			=> 'Barbecuing',
+    'beaurocracy'			=> 'Bureaucracy',
+    'bedouin'				=> 'Bedouins',
+    'bedouins'				=> 'Bedouins',
+    'beduin'				=> 'Bedouins',
+    'beduins'				=> 'Bedouins',
+    'beer'				=> 'Beer',
+    'beer--use in cooking'		=> 'Cooking (Beer)',
+    "beginner's all-purpose symbolic instruction code"	=> 'BASIC (Computer program language)',
+    'behavior in organizations'		=> 'Organizational behavior',
+    'beverages'				=> 'Beverages',
+    'beverages, non-alcoholic'		=> 'Non-alcoholic beverages',
+    'bi-sexuality'			=> 'Bisexuality',
+    'bible'				=> 'Bible',
+    'big guns'				=> 'Ordnance',
+    'bio-informatics'			=> 'Bioinformatics',
+    'bio-terrorism'			=> 'Bioterrorism',
+    'biography'				=> 'Biography',
+    'bioinformatics'			=> 'Bioinformatics',
+    'biological informatics'		=> 'Bioinformatics',
+    'biological neural networks'	=> 'Neural networks (Neurobiology)',
+    'bionics'				=> 'Bionics',
+    'biotechnology industries'		=> 'Biotechnology industries',
+    'biotechnology'			=> 'Biotechnology',
+    'bioterrorism'			=> 'Bioterrorism',
+    'bioterrorism--social aspects'	=> 'Bioterrorism--Social aspects',
+    'biscuits, english'			=> 'Cookies',
+    'bisexuality'			=> 'Bisexuality',
+    'blogging'				=> 'Blogs',
+    'blogs'				=> 'Blogs',
+    'boats'				=> 'Boats',
+    'body art'				=> 'Body art',
+    'body building'			=> 'Bodybuilding',
+    'body care'				=> 'Hygiene',
+    'body sculpting'			=> 'Bodybuilding',
+    'body sculpting (bodybuilding)'	=> 'Bodybuilding',
+    'bodybuilding'			=> 'Bodybuilding',
+    'bodysculpting'			=> 'Bodybuilding',
+    'bodysculpting (bodybuilding)'	=> 'Bodybuilding',
+    'bond issues'			=> 'Bonds',
+    'bonds'				=> 'Bonds',
+    'bookkeeping'			=> 'Bookkeeping',
+    'books'				=> 'Books',
+    'books--appraisal'			=> 'Criticism',
+    'bottles'				=> 'Bottles',
+    'bread'				=> 'Bread',
+    'bread--use in cooking'		=> 'Cooking (Bread)',
+    'breads'				=> 'Bread',
+    'breakfasts'			=> 'Breakfasts',
+    'breton cooking'			=> 'Cooking, French--Brittany style',
+    'bric-a-brac'			=> 'Art objects',
+    'brittany cooking'			=> 'Cooking, French--Brittany style',
+    'broadband communication systems'	=> 'Broadband communication systems',
+    'browsers (computer programs)'	=> 'Browsers (Computer programs)',
+    'brunches'				=> 'Brunches',
+    'budget'				=> 'Budget',
+    'budgeting'				=> 'Budget',
+    'budgets, family'			=> 'Budgets, Personal',
+    'budgets, personal'			=> 'Budgets, Personal',
+    'budgets, time'			=> 'Time management',
+    'bureaucracy'			=> 'Bureaucracy',
+    'burgundy cooking'			=> 'Cooking, French--Burgundy style',
+    'business'				=> 'Commerce',
+    'business administration'		=> 'Industrial management',
+    'business and art'			=> 'Art and business',
+    'business coaching'			=> 'Executive coaching',
+    'business combinations'		=> 'Consolidation and merger of corporations',
+    'business communication'		=> 'Business communication',
+    'business data interchange, electronic' => 'Electronic data interchange',
+    'business development'		=> 'Business--Development',
+    'business education'		=> 'Business education',
+    'business enterprises'		=> 'Business enterprises',
+    'business enterprises, home'	=> 'Home-based businesses',
+    'business enterprises, international' => 'International business enterprises',
+    'business enterprises--environmental aspects' =>
+      'Business enterprises--Environmental aspects',
+    'business enterprises--management'	=> 'Industrial management',
+    'business espionage'		=> 'Business intelligence',
+    'business ethics'			=> 'Business ethics',
+    'business etiquette'		=> 'Business etiquette',
+    'business finance'			=> 'Corporations--Finance',
+    'business forecasting'		=> 'Business forecasting',
+    'business forecasts'		=> 'Business forecasting',
+    'business graphics software'	=> 'Presentation graphics software',
+    'business history'			=> 'Commerce--History',
+    'business intelligence'		=> 'Business intelligence',
+    'business law'			=> 'Commercial law',
+    'business machines'			=> 'Office equipment and supplies',
+    'business management'		=> 'Industrial management',
+    'business math'			=> 'Business mathematics',
+    'business mathematics'		=> 'Business mathematics',
+    'business meetings'			=> 'Business meetings',
+    'business mergers'			=> 'Consolidation and merger of corporations',
+    'business organizations'		=> 'Business enterprises',
+    'business presentation software'	=> 'Presentation graphics software',
+    'business starts'			=> 'New business enterprises',
+    'business statistics'		=> 'Commercial statistics',
+    'business to business marketing'	=> 'Industrial marketing',
+    'business training'			=> 'Business--Training',
+    'business writing'			=> 'Business writing',
+    'business--development'		=> 'Business--Development',
+    'business--forecasting'		=> 'Business forecasting',
+    'business--government policy'	=> 'Industrial policy',
+    'business--public relations'	=> 'Public relations',
+    'business--reference books'		=> 'Business--Reference books',
+    'business--statistical methods'	=> 'Commercial statistics',
+    'business--statistics'		=> 'Commercial statistics',
+    'business--study and teaching'	=> 'Business education',
+    'business--training'		=> 'Business--Training',
+    'business-to-business marketing'	=> 'Industrial marketing',
+    'businesses'			=> 'Business enterprises',
+    'businesses, home'			=> 'Home-based businesses',
+    'businesses, small'			=> 'Small business',
+    'businesswomen'			=> 'Businesswomen',
+    'buttons'				=> 'Buttons',
+    'buyer behavior'			=> 'Consumer behavior',
+    "buyers' guides"			=> "Buyers' guides",
+    'buying'				=> 'Purchasing',
+    'buyouts, corporate'		=> 'Consolidation and merger of corporations',
+    'c (computer program language)'	=> 'C (Computer program language)',
+    'c plus plus'			=> 'C++ (Computer program language)',
+    'c# (computer program language)'	=> 'C# (Computer program language)',
+    'c++ (computer program language)'	=> 'C++ (Computer program language)',
+    'c++'				=> 'C++ (Computer program language)',
+    'c-sharp (computer program language)' => 'C# (Computer program language)',
+    'cad'				=> 'Computer-aided design',
+    'cad/cam systems'			=> 'CAD/CAM systems',
+    'cajan cooking'			=> 'Cooking, Cajun',
+    'cajun cooking'			=> 'Cooking, Cajun',
+    'cake'				=> 'Cake',
+    'cakes'				=> 'Cake',
+    'calculating machines'		=> 'Calculators',
+    'calculating-machines'		=> 'Calculators',
+    'calculators'			=> 'Calculators',
+    'california cooking'		=> 'Cooking, American--California style',
+    'call options'			=> 'Options (Finance)',
+    'calligraphy'			=> 'Calligraphy',
+    'calligraphy--technique'		=> 'Calligraphy--Technique',
+    'calls (finance)'			=> 'Options (Finance)',
+    'caloric content of food'		=> 'Food--Caloric content',
+    'caloric content of foods'		=> 'Food--Caloric content',
+    'calories (food)'			=> 'Food--Caloric content',
+    'cambistry'				=> 'Foreign exchange',
+    'camellia sinensis'			=> 'Tea',
+    'camellia thea'			=> 'Tea',
+    'camellia theifera'			=> 'Tea',
+    'cameralistics'			=> 'Finance, Public',
+    'camp cooking'			=> 'Outdoor cooking',
+    'campfire cooking'			=> 'Outdoor cooking',
+    'canadian art'			=> 'Art, Canadian',
+    'canadian cooking'			=> 'Cooking, Canadian',
+    'canapes'				=> 'Appetizers',
+    'canapés'				=> 'Appetizers',
+    'cancer'				=> 'Cancer',
+    'cancer--nutritional aspects'	=> 'Cancer--Nutritional aspects',
+    'cancers'				=> 'Cancer',
+    'canning and preserving'		=> 'Canning and preserving',
+    'cannon'				=> 'Ordnance',
+    'cannons'				=> 'Ordnance',
+    'capital equipment'			=> 'Industrial equipment',
+    'capital goods'			=> 'Industrial equipment',
+    'capital punishment'		=> 'Capital punishment',
+    'capital punishment, abolition of'	=> 'Capital punishment',
+    'capital, intellectual'		=> 'Intellectual capital',
+    'capital, knowledge'		=> 'Intellectual capital',
+    'carcinoma'				=> 'Cancer',
+    'careers'				=> 'Occupations',
+    'caribbean art'			=> 'Art, Caribbean',
+    'caribbean cooking'			=> 'Cooking, Caribbean',
+    'caricatures and cartoons'		=> 'Caricatures and cartoons',
+    'cars (automobiles)'		=> 'Automobiles',
+    'cars'				=> 'Automobiles',
+    'cars, antique'			=> 'Antique and classic cars',
+    'cars, classic'			=> 'Antique and classic cars',
+    'cars, vintage'			=> 'Antique and classic cars',
+    'cartooning'			=> 'Cartooning',
+    'cartooning--technique'		=> 'Cartooning',
+    'cartoons'				=> 'Caricatures and cartoons',
+    'carvings'				=> 'Sculpture',
+    'case studies'			=> 'Case studies',
+    'casserole cookery'			=> 'Casserole cooking',
+    'casserole cooking'			=> 'Casserole cooking',
+    'casualty insurance'		=> 'Casualty insurance',
+    'cats'				=> 'Cats',
+    'cds'	       			=> 'Compact discs',
+    'ceramic art'			=> 'Pottery',
+    'ceramics (art)'			=> 'Pottery',
+    'ceramics'				=> 'Pottery',
+    'certification'			=> 'Certification',
+    'change, organizational'		=> 'Organizational change',
+    'channeling (spiritualism)'		=> 'Channeling (Spiritualism)',
+    'channelling (spiritualism)'	=> 'Channeling (Spiritualism)',
+    'cheirognomy'			=> 'Palmistry',
+    'cheiromancy'			=> 'Palmistry',
+    'cheirosophy'			=> 'Palmistry',
+    'cherubim'				=> 'Angels',
+    'cherubs (spirits)'			=> 'Angels',
+    'cherubs'				=> 'Angels',
+    'child abuse'			=> 'Child abuse',
+    'child maltreatment'		=> 'Child abuse',
+    'child molestation'			=> 'Child sexual abuse',
+    'child molesting'			=> 'Child sexual abuse',
+    'child neglect'			=> 'Child abuse',
+    'child sexual abuse'		=> 'Child sexual abuse',
+    'children'				=> 'Children',
+    'children--abuse of'		=> 'Child abuse',
+    'chinaware'				=> 'Pottery',
+    'chinese astrology'			=> 'Astrology, Chinese',
+    'chinese cooking'			=> 'Cooking, Chinese',
+    'chinese historical fiction'	=> 'Historical fiction, Chinese',
+    'chirognomy'			=> 'Palmistry',
+    'chiromancy'			=> 'Palmistry',
+    'chirosophy'			=> 'Palmistry',
+    'chocolate'				=> 'Chocolate',
+    'chocolate--use in cooking'		=> 'Cooking (Chocolate)',
+    # 'chowders' could map to either 'Soups' or 'Stews'
+    'christianity'			=> 'Christianity',
+    'cipher stories'			=> 'Code and cipher stories',
+    'ciphers'				=> 'Ciphers',
+    'ciphers--fiction'			=> 'Code and cipher stories',
+    'ciphers--juvenile fiction'		=> 'Code and cipher stories',
+    'cities and towns'			=> 'Cities and towns',
+    'cities and towns--economic aspects' => 'Urban economics',
+    'cities'				=> 'Cities and towns',
+    'cities, imaginary'			=> 'Imaginary places',
+    'city economics'			=> 'Urban economics',
+    'city of london (england)'		=> 'City of London (England)',
+    'civil liberties'			=> 'Civil rights',
+    'civil resistance'			=> 'Government, Resistance to',
+    'civil rights'			=> 'Civil rights',
+    'class distinction'			=> 'Social classes',
+    'classes, social'			=> 'Social classes',
+    'classic automobiles'		=> 'Antique and classic cars',
+    'classic cars'			=> 'Antique and classic cars',
+    'cleanliness'			=> 'Hygiene',
+    'climate science'			=> 'Climatology',
+    'climate'				=> 'Weather',
+    'climate, workplace'		=> 'Work environment',
+    'climatology'			=> 'Climatology',
+    'clinical informatics'		=> 'Medical informatics',
+    'clinical sciences'			=> 'Medicine',
+    'clocks and watches'		=> 'Clocks and watches',
+    'clocks'				=> 'Clocks and watches',
+    'clones of macintosh computers'	=> 'Macintosh-compatible computers',
+    'coaching, business'		=> 'Executive coaching',
+    'coaching, executive'		=> 'Executive coaching',
+    'cobol (computer program language)'	=> 'COBOL (Computer program language)',
+    'cobol'				=> 'COBOL (Computer program language)',
+    'code and cipher stories'		=> 'Code and cipher stories',
+    'code stories'			=> 'Code and cipher stories',
+    'codes'				=> 'Ciphers',
+    'coffea arabica'			=> 'Coffee',
+    'coffea'				=> 'Coffee',
+    'coffee'				=> 'Coffee',
+    'coffee--use in cooking'		=> 'Cooking (Coffee)',
+    'cognitive psychology'		=> 'Cognitive psychology',
+    'coins'				=> 'Coins',
+    'cold war'				=> 'Cold War',
+    'collectables'			=> 'Collectibles',
+    'collectibles'			=> 'Collectibles',
+    'collectibles, political'		=> 'Political collectibles',
+    'collectibles--popular culture'	=> 'Collectibles--Popular culture',
+    'collision insurance, automobile'	=> 'Automobile insurance',
+    'color in art'			=> 'Color in art',
+    'colors in art'			=> 'Color in art',
+    'comic book heroes'			=> 'Superheroes',
+    'comic books'			=> 'Comic books, strips, etc',
+    'comic books, strips, etc'		=> 'Comic books, strips, etc',
+    'comic strips'			=> 'Comic books, strips, etc',
+    'comics'				=> 'Comic books, strips, etc',
+    'coming of age'			=> 'Coming of age',
+    'command of troops'			=> 'Command of troops',
+    'command of troops--case studies'	=> 'Command of troops--Case studies',
+    'commerce'				=> 'Commerce',
+    'commerce--history'			=> 'Commerce--History',
+    'commerce--statistical methods'	=> 'Commercial statistics',
+    'commerce--statistics'		=> 'Commercial statistics',
+    'commercial accounting'		=> 'Accounting',
+    'commercial education'		=> 'Business education',
+    'commercial ethics'			=> 'Business ethics',
+    'commercial goods'			=> 'Commercial products',
+    'commercial law'			=> 'Commercial law',
+    'commercial policy'			=> 'Commercial policy',
+    'commercial products'		=> 'Commercial products',
+    'commercial property'		=> 'Commercial real estate',
+    'commercial real estate'		=> 'Commercial real estate',
+    'commercial real property'		=> 'Commercial real estate',
+    'commercial space (real estate)'	=> 'Commercial real estate',
+    'commercial space'			=> 'Commercial real estate',
+    'commercial speech'			=> 'Advertising',
+    'commercial statistics'		=> 'Commercial statistics',
+    'commercial trade'			=> 'Commerce',
+    'commodities exchange'		=> 'Commodity exchanges',
+    'commodities exchanges'		=> 'Commodity exchanges',
+    'commodities'			=> 'Commercial products',
+    'commodity exchanges'		=> 'Commodity exchanges',
+    'commodity markets'			=> 'Commodity exchanges',
+    'common business-oriented language'	=> 'COBOL (Computer program language)',
+    'common shares'			=> 'Stocks',
+    'common stocks'			=> 'Stocks',
+    'communication and traffic'		=> 'Communication and traffic',
+    'communication systems, computer'	=> 'Computer networks',
+    'communication systems, wireless'	=> 'Wireless communication systems',
+    'communication theory'		=> 'Information theory',
+    'communication, administrative'	=> 'Business communication',
+    'communication, business'		=> 'Business communication',
+    'communication, industrial'		=> 'Business communication',
+    'communications industries'		=> 'Communication and traffic',
+    'community health'			=> 'Public health',
+    'compact discs'			=> 'Compact discs',
+    'compact disks'			=> 'Compact discs',
+    'companies'				=> 'Business enterprises',
+    'comparative economic systems'	=> 'Comparative economics',
+    'comparative economics'		=> 'Comparative economics',
+    'compatible computers, macintosh'	=> 'Macintosh-compatible computers',
+    'competitive intelligence'		=> 'Business intelligence',
+    'compilers (computer programs)'	=> 'Compilers (Computer programs)',
+    'compilers'				=> 'Compilers (Computer programs)',
+    'compiling programs'		=> 'Compilers (Computer programs)',
+    'computer graphics'			=> 'Computer graphics',
+    "computers--buyers' guides"		=> "Computers--Buyers' guides",
+    'competition, international'	=> 'Competition, International',
+    'comprehensive health planning'	=> 'Health planning',
+    'computational linguistics'		=> 'Computational linguistics',
+    'computer aided design'		=> 'Computer-aided design',
+    'computer aided manufacturing systems' => 'CAD/CAM systems',
+    'computer animation'		=> 'Computer animation',
+    'computer architecture'		=> 'Computer architecture',
+    'computer assisted design'		=> 'Computer-aided design',
+    'computer assisted animation'	=> 'Computer animation',
+    'computer assisted filmmaking'	=> 'Computer animation',
+    'computer code, malicious'		=> 'Malware (Computer software)',
+    'computer communication systems'	=> 'Computer networks',
+    'computer crimes'			=> 'Computer crimes',
+    'computer engineering'		=> 'Computer engineering',
+    'computer game programming'		=> 'Computer games--Programming',
+    'computer games'			=> 'Computer games',
+    'computer games--programming'	=> 'Computer games--Programming',
+    'computer hardware'			=> 'Computers',
+    'computer history'			=> 'Computers--History',
+    'computer industry'			=> 'Computer industry',
+    'computer languages'		=> 'Programming languages (Electronic computers)',
+    'computer literacy'			=> 'Computer literacy',
+    'computer modeling'			=> 'Computer simulation',
+    'computer models'			=> 'Computer simulation',
+    'computer network protocols'	=> 'Computer network protocols',
+    'computer network security'		=> 'Computer networks--Security measures',
+    'computer networks'			=> 'Computer networks',
+    'computer networks--security measures' => 'Computer networks--Security measures',
+    'computer operating systems'	=> 'Operating systems (Computers)',
+    'computer peripherals'		=> 'Computer peripherals',
+    'computer privacy'			=> 'Computer security',
+    'computer program languages'	=> 'Programming languages (Electronic computers)',
+    'computer programming languages'	=> 'Programming languages (Electronic computers)',
+    'computer programming'		=> 'Computer programming',
+    'computer programming--algorithms'	=> 'Computer programming--Algorithms',
+    'computer science'			=> 'Computer science',
+    'computer security'			=> 'Computer security',
+    'computer simulation'		=> 'Computer simulation',
+    'computer software'			=> 'Computer software',
+    'computer software--development'	=> 'Computer software--Development',
+    'computer software--education'	=> 'Computer software--Education',
+    'computer software--mathematics'	=> 'Computer software--Mathematics',
+    'computer software--quality assurance' => 'Computer software--Quality assurance',
+    'computer system security'		=> 'Computer security',
+    'computer systems--security measures' => 'Computer security',
+    'computer systems--security'	=> 'Computer security',
+    'computer technicians'		=> 'Computer technicians',
+    'computer technicians--certification--study guides' =>
+      'Computer technicians--Certification--Study guides',
+    'computer technologists'		=> 'Computer technicians',
+    'computer vision'			=> 'Computer vision',
+    'computer-aided design'		=> 'Computer-aided design',
+    'computer-aided manufacturing systems' => 'CAD/CAM systems',
+    'computer-assisted design'		=> 'Computer-aided design',
+    'computer-assisted animation'	=> 'Computer animation',
+    'computer-assisted filmmaking'	=> 'Computer animation',
+    'computer-generated animation'	=> 'Computer animation',
+    'computer-human interaction'	=> 'Human-computer interaction',
+    'computers'				=> 'Computers',
+    'computers, mechanical'		=> 'Calculators',
+    'computers--administration'		=> 'Computers--Administration',
+    'computers--design and construction' => 'Computer engineering',
+    'computers--history'		=> 'Computers--History',
+    'computers--operating systems'	=> 'Operating systems (Computers)',
+    'computers--programming'		=> 'Computer programming',
+    'computers--reference'		=> 'Computers--Reference',
+    'computers--security measures'	=> 'Computer security',
+    'computers--security'		=> 'Computer security',
+    'computers--social aspects'		=> 'Computers--Social aspects',
+    'computing history'			=> 'Computers--History',
+    'computing machines'		=> 'Computers',
+    'concatenation, file (statistics)'	=> 'Statistical matching',
+    'concept art'			=> 'Conceptual art',
+    'conceptual art'			=> 'Conceptual art',
+    'condiments'			=> 'Condiments',
+    'confectionary'			=> 'Confectionery',
+    'confectionery'			=> 'Confectionery',
+    'confections'			=> 'Confectionery',
+    'confederate cooking'		=> 'Cooking, American--Southern style',
+    'conflict control'			=> 'Conflict management',
+    'conflict management'		=> 'Conflict management',
+    'conflict resolution'		=> 'Conflict management',
+    'consciousness'			=> 'Consciousness',
+    'consciousness-expanding drugs'	=> 'Hallucinogenic drugs',
+    'conservation of art objects'       => 'Art objects--Conservation and restoration',
+    'consolidation and merger of corporations' => 'Consolidation and merger of corporations',
+    'conspiracy'			=> 'Conspiracy',
+    'constitutional rights'		=> 'Civil rights',
+    'consumer advertising'		=> 'Advertising',
+    'consumer behavior'			=> 'Consumer behavior',
+    'consumer goods--marketing'		=> 'Marketing',
+    'contract services'			=> 'Contracting out',
+    'contracting for services'		=> 'Contracting out',
+    'contracting out'			=> 'Contracting out',
+    'contractions (topology)'		=> 'Contractions (Topology)',
+    'controversial literature'		=> 'Controversial literature',
+    'convenience cooking'		=> 'Quick and easy cooking',
+    'cookery (appetizers)'		=> 'Appetizers',
+    'cookery (baby foods)'		=> 'Baby foods',
+    'cookery (beer)'			=> 'Cooking (Beer)',
+    'cookery (bread)'			=> 'Cooking (Bread)',
+    'cookery (chocolate)'		=> 'Cooking (Chocolate)',
+    'cookery (coffee)'			=> 'Cooking (Coffee)',
+    'cookery (entrées)'			=> 'Entrées (Cooking)',
+    'cookery (garnishes)'		=> 'Garnishes (Cooking)',
+    'cookery (herbs)'			=> 'Cooking (Herbs)',
+    'cookery (liquors)'			=> 'Cooking (Liquors)',
+    'cookery (spices)'			=> 'Cooking (Spices)',
+    'cookery (tea)'			=> 'Cooking (Tea)',
+    'cookery for one'			=> 'Cooking for one',
+    'cookery'				=> 'Cooking',
+    'cookery, african'			=> 'Cooking, African',
+    'cookery, american'			=> 'Cooking, American',
+    'cookery, asian'			=> 'Cooking, Asian',
+    'cookery, cajan'			=> 'Cooking, Cajun',
+    'cookery, cajun'			=> 'Cooking, Cajun',
+    'cookery, canadian'			=> 'Cooking, Canadian',
+    'cookery, caribbean'		=> 'Cooking, Caribbean',
+    'cookery, chinese'			=> 'Cooking, Chinese',
+    'cookery, english'			=> 'Cooking, English',
+    'cookery, european'			=> 'Cooking, European',
+    'cookery, french'			=> 'Cooking, French',
+    'cookery, french-canadian'		=> 'Cooking, French-Canadian',
+    'cookery, german'			=> 'Cooking, German',
+    'cookery, greek'			=> 'Cooking, Greek',
+    'cookery, latin american'		=> 'Cooking, Latin American',
+    'cookery, oriental'			=> 'Cooking, Asian',
+    'cookery, provencal'		=> 'Cooking, French--Provençal style',
+    'cookery, provençal'		=> 'Cooking, French--Provençal style',
+    'cookery, scottish'			=> 'Cooking, Scottish',
+    'cookery, welsh'			=> 'Cooking, Welsh',
+    'cookery, west indian'		=> 'Cooking, West Indian',
+    'cookery--herbs'			=> 'Cooking (Herbs)',
+    'cookery--history'			=> 'Cooking--History',
+    'cookies (computer science)'	=> 'Cookies (Computer science)',
+    'cookies'				=> 'Cookies',
+    'cooking (appetizers)'		=> 'Appetizers',
+    'cooking (beer)'			=> 'Cooking (Beer)',
+    'cooking (bread)'			=> 'Cooking (Bread)',
+    'cooking (chocolate)'		=> 'Cooking (Chocolate)',
+    'cooking (coffee)'			=> 'Cooking (Coffee)',
+    'cooking (entrées)'			=> 'Entrées (Cooking)',
+    'cooking (garnishes)'		=> 'Garnishes (Cooking)',
+    'cooking (herbs)'			=> 'Cooking (Herbs)',
+    'cooking (liquors)'			=> 'Cooking (Liquors)',
+    'cooking (spices)'			=> 'Cooking (Spices)',
+    'cooking (spirits)'			=> 'Cooking (Liquors)',
+    'cooking (spirits, alcoholic)'	=> 'Cooking (Liquors)',
+    'cooking (tea)'			=> 'Cooking (Tea)',
+    'cooking bread'			=> 'Cooking (Bread)',
+    'cooking for one'			=> 'Cooking for one',
+    'cooking for large numbers'		=> 'Quantity cooking',
+    'cooking for many people'		=> 'Quantity cooking',
+    'cooking history'			=> 'Cooking--History',
+    'cooking utensils'			=> 'Kitchen utensils',
+    'cooking with beer'			=> 'Cooking (Beer)',
+    'cooking with bread'		=> 'Cooking (Bread)',
+    'cooking with chocolate'		=> 'Cooking (Chocolate)',
+    'cooking with coffee'		=> 'Cooking (Coffee)',
+    'cooking with herbs'		=> 'Cooking (Herbs)',
+    'cooking with liquors'		=> 'Cooking (Liquors)',
+    'cooking with spices'		=> 'Cooking (Spices)',
+    'cooking with tea'			=> 'Cooking (Tea)',
+    'cooking'				=> 'Cooking',
+    'cooking, african'			=> 'Cooking, African',
+    'cooking, american'			=> 'Cooking, American',
+    'cooking, american--california style'	=> 'Cooking, American--California style',
+    'cooking, american--confederate style'	=> 'Cooking, American--Southern style',
+    'cooking, american--middle atlantic states'	=> 'Cooking, American--Middle Atlantic States',
+    'cooking, american--middle western style'	=> 'Cooking, American--Midwestern style',
+    'cooking, american--midwestern style'	=> 'Cooking, American--Midwestern style',
+    'cooking, american--new england style'	=> 'Cooking, American--New England style',
+    'cooking, american--northwestern states'	=> 'Cooking, American--Northwestern States',
+    'cooking, american--southern style'	=> 'Cooking, American--Southern style',
+    'cooking, american--southwestern style'	=> 'Cooking, American--Southwestern style',
+    'cooking, american--western style'	=> 'Cooking, American--Western style',
+    'cooking, alsatian'			=> 'Cooking, French--Alsatian style',
+    'cooking, asian'			=> 'Cooking, Asian',
+    'cooking, auvergnat'		=> 'Cooking, French--Auvergne style',
+    'cooking, auvergne'			=> 'Cooking, French--Auvergne style',
+    'cooking, bavarian'			=> 'Cooking, German--Bavarian style',
+    'cooking, breton'			=> 'Cooking, French--Brittany style',
+    'cooking, brittany'			=> 'Cooking, French--Brittany style',
+    'cooking, burgundy'			=> 'Cooking, French--Burgundy style',
+    'cooking, cajan'			=> 'Cooking, Cajun',
+    'cooking, cajun'			=> 'Cooking, Cajun',
+    'cooking, california'		=> 'Cooking, American--California style',
+    'cooking, canadian'			=> 'Cooking, Canadian',
+    'cooking, caribbean'		=> 'Cooking, Caribbean',
+    'cooking, central american and south american'	=> 'Cooking, Latin American',
+    'cooking, central and south american'	=> 'Cooking, Latin American',
+    'cooking, chinese'			=> 'Cooking, Chinese',
+    'cooking, confederate style'	=> 'Cooking, American--Southern style',
+    'cooking, confederate'		=> 'Cooking, American--Southern style',
+    'cooking, cornish'			=> 'Cooking, English--Cornish style',
+    'cooking, corsican'			=> 'Cooking, French--Corsican style',
+    'cooking, creole'			=> 'Cooking, Creole',
+    'cooking, cretan'			=> 'Cooking, Greek--Cretan style',
+    'cooking, devon'			=> 'Cooking, English--Devon style',
+    'cooking, devonshire'		=> 'Cooking, English--Devon style',
+    'cooking, english'			=> 'Cooking, English',
+    'cooking, english--cornish style'	=> 'Cooking, English--Cornish style',
+    'cooking, english--devon style'	=> 'Cooking, English--Devon style',
+    'cooking, european'			=> 'Cooking, European',
+    'cooking, french'			=> 'Cooking, French',
+    'cooking, french--alsatian style'	=> 'Cooking, French--Alsatian style',
+    'cooking, french--auvergne style'	=> 'Cooking, French--Auvergne style',
+    'cooking, french--brittany style'	=> 'Cooking, French--Brittany style',
+    'cooking, french--burgundy style'	=> 'Cooking, French--Burgundy style',
+    'cooking, french--corsican style'	=> 'Cooking, French--Corsican style',
+    'cooking, french--gascony style'	=> 'Cooking, French--Gascony style',
+    'cooking, french--lorraine style'	=> 'Cooking, French--Lorraine style',
+    'cooking, french--normandy style'	=> 'Cooking, French--Normandy style',
+    'cooking, french--provencal style'	=> 'Cooking, French--Provençal style',
+    'cooking, french--provençal style'	=> 'Cooking, French--Provençal style',
+    'cooking, french-canadian'		=> 'Cooking, French-Canadian',
+    'cooking, gascon'			=> 'Cooking, French--Gascony style',
+    'cooking, gascony'			=> 'Cooking, French--Gascony style',
+    'cooking, german'			=> 'Cooking, German',
+    'cooking, german--bavarian style'	=> 'Cooking, German--Bavarian style',
+    'cooking, german--schleswig-holstein style'	=> 'Cooking, German--Schleswig-Holstein style',
+    'cooking, german--southern style'	=> 'Cooking, German--Southern style',
+    'cooking, german--westphalian style'	=> 'Cooking, German--Westphalian style',
+    'cooking, greek'			=> 'Cooking, Greek',
+    'cooking, greek--cretan style'	=> 'Cooking, Greek--Cretan style',
+    'cooking, latin american'		=> 'Cooking, Latin American',
+    'cooking, lorraine'			=> 'Cooking, French--Lorraine style',
+    'cooking, microwave'		=> 'Microwave cooking',
+    'cooking, midwestern style'		=> 'Cooking, American--Midwestern style',
+    'cooking, midwestern'		=> 'Cooking, American--Midwestern style',
+    'cooking, new england style'	=> 'Cooking, American--New England style',
+    'cooking, new england'		=> 'Cooking, American--New England style',
+    'cooking, norman'			=> 'Cooking, French--Normandy style',
+    'cooking, normandy'			=> 'Cooking, French--Normandy style',
+    'cooking, northwestern states'	=> 'Cooking, American--Northwestern States',
+    'cooking, oriental'			=> 'Cooking, Asian',
+    'cooking, provencal'		=> 'Cooking, French--Provençal style',
+    'cooking, provençal'		=> 'Cooking, French--Provençal style',
+    'cooking, schleswig-holstein'	=> 'Cooking, German--Schleswig-Holstein style',
+    'cooking, scottish'			=> 'Cooking, Scottish',
+    'cooking, southern (united states)'	=> 'Cooking, American--Southern style',
+    'cooking, southern (germany)'	=> 'Cooking, German--Southern style',
+    'cooking, southwestern (united states)'	=> 'Cooking, American--Southwestern style',
+    'cooking, welsh'			=> 'Cooking, Welsh',
+    'cooking, west indian'		=> 'Cooking, West Indian',
+    'cooking, western (united states)'	=> 'Cooking, American--Western style',
+    'cooking, westphalian'		=> 'Cooking, German--Westphalian style',
+    'cooking--herbs'			=> 'Cooking (Herbs)',
+    'cooking--history'			=> 'Cooking--History',
+    'cornish cooking'			=> 'Cooking, English--Cornish style',
+    'corporate acquisitions'		=> 'Consolidation and merger of corporations',
+    'corporate buyouts'			=> 'Consolidation and merger of corporations',
+    'corporate coaching'		=> 'Executive coaching',
+    'corporate espionage'		=> 'Business intelligence',
+    'corporate ethics'			=> 'Business ethics',
+    'corporate finance'			=> 'Corporations--Finance',
+    'corporate financial management'	=> 'Corporations--Finance',
+    'corporate governance'		=> 'Corporate governance',
+    'corporate income tax'		=> 'Corporations--Taxation',
+    'corporate income taxes'		=> 'Corporations--Taxation',
+    'corporate intelligence'		=> 'Business intelligence',
+    'corporate management'		=> 'Industrial management',
+    'corporate mergers'			=> 'Consolidation and merger of corporations',
+    'corporate power'			=> 'Corporate power',
+    'corporate takeovers'		=> 'Consolidation and merger of corporations',
+    'corporate tax'			=> 'Corporations--Taxation',
+    'corporate taxes'			=> 'Corporations--Taxation',
+    'corporation ethics'		=> 'Business ethics',
+    'corporation finance'		=> 'Corporations--Finance',
+    'corporation income tax'		=> 'Corporations--Taxation',
+    'corporation income taxes'		=> 'Corporations--Taxation',
+    'corporation tax'			=> 'Corporations--Taxation',
+    'corporation taxes'			=> 'Corporations--Taxation',
+    'corporations'			=> 'Corporations',
+    'corporations, federal'		=> 'Corporations, Government',
+    'corporations, government'		=> 'Corporations, Government',
+    'corporations, international'	=> 'International business enterprises',
+    'corporations, nonprofit'		=> 'Nonprofit organizations',
+    'corporations, religious'		=> 'Corporations, Religious',
+    'corporations--consolidation'	=> 'Consolidation and merger of corporations',
+    'corporations--finance'		=> 'Corporations--Finance',
+    'corporations--management'		=> 'Industrial management',
+    'corporations--mergers'		=> 'Consolidation and merger of corporations',
+    'corporations--taxation'		=> 'Corporations--Taxation',
+    'corpulence'			=> 'Obesity',
+    'correspondence'			=> 'Letters',
+    'corrupt practices'			=> 'Corruption',
+    'corruption'			=> 'Corruption',
+    'corsican cooking'			=> 'Cooking, French--Corsican style',
+    'county parks'			=> 'Parks',
+    'courting'				=> 'Courtship',
+    'courtship'				=> 'Courtship',
+    'courtship--fiction'		=> 'Courtship--Fiction',
+    'coverage, insurance'		=> 'Insurance',
+    'creole cooking'			=> 'Cooking, Creole',
+    'cretan cooking'			=> 'Cooking, Greek--Cretan style',
+    'criminals'				=> 'Criminals',
+    'criminals--biography'		=> 'Criminals--Biography',
+    'criticism'				=> 'Criticism',
+    'crockery'				=> 'Pottery',
+    'crockett, david, 1786-1836'	=> 'Crockett, Davy, 1786-1836',
+    'crockett, davy, 1786-1836'		=> 'Crockett, Davy, 1786-1836',
+    'cruelty to children'		=> 'Child abuse',
+    'cryptanalysis'			=> 'Cryptography',
+    'cryptogram stories'		=> 'Code and cipher stories',
+    'cryptography'			=> 'Cryptography',
+    'cryptography--fiction'		=> 'Code and cipher stories',
+    'cryptography--juvenile fiction'	=> 'Code and cipher stories',
+    'cryptology'			=> 'Cryptography',
+    'crystals'				=> 'Crystals',
+    'cuisine'				=> 'Cooking',
+    'culture and politics'		=> 'Politics and culture',
+    'culture conflict'			=> 'Culture conflict',
+    'culture, popular'			=> 'Popular culture',
+    'culture--political aspects'	=> 'Politics and culture',
+    'currency exchange'			=> 'Foreign exchange',
+    'currency'				=> 'Money',
+    'curricula vitae'			=> 'Résumés (Employment)',
+    'customer relations'		=> 'Customer relations',
+    'cyber casting'			=> 'Webcasting',
+    'cyber commerce'			=> 'Electronic commerce',
+    'cyber crime'			=> 'Computer crimes',
+    'cybercasting'			=> 'Webcasting',
+    'cybercommerce'			=> 'Electronic commerce',
+    'cybercrime'			=> 'Computer crimes',
+    'cybernetics'			=> 'Cybernetics',
+    'cyborg'				=> 'Cyborgs',
+    'cyborgs'				=> 'Cyborgs',
+    'c♯ (computer program language)'	=> 'C# (Computer program language)',
+    'daniels, kate (fictitious character)'	=> 'Daniels, Kate (Fictitious character)',
+    'daniels, kate--fiction'		=> 'Daniels, Kate (Fictitious character)',
+    'data banks'			=> 'Databases',
+    'data bases'			=> 'Databases',
+    'data matching (statistics)'	=> 'Statistical matching',
+    'data matching'			=> 'Statistical matching',
+    'data merging (statistics)'		=> 'Statistical matching',
+    'data merging'			=> 'Statistical matching',
+    'data mining'			=> 'Data mining',
+    'data networks'			=> 'Computer networks',
+    'data networks, computer'		=> 'Computer networks',
+    'data processing equipment'		=> 'Computers',
+    'data processing'			=> 'Data processing',
+    'data communication systems'	=> 'Data transmission systems',
+    'data transmission systems'		=> 'Data transmission systems',
+    'data transmission systems, broadband' => 'Broadband communication systems',
+    'data transmission systems, wideband' => 'Broadband communication systems',
+    'data transmission systems, wireless' => 'Wireless communication systems',
+    'data warehousing'			=> 'Data warehousing',
+    'databanks'				=> 'Databases',
+    'database systems'			=> 'Databases',
+    'databases'				=> 'Databases',
+    'datamining'			=> 'Data mining',
+    'death (personification)'		=> 'Death (Personification)',
+    'death penalty'			=> 'Capital punishment',
+    'death sentence'			=> 'Capital punishment',
+    'death'				=> 'Death',
+    'death--philosophy'			=> 'Death',
+    'debentures'			=> 'Bonds',
+    'debian (computer system)'		=> 'Debian (Computer system)',
+    'debian'				=> 'Debian (Computer system)',
+    'deciding'				=> 'Decision making',
+    'decision (psychology)'		=> 'Decision making',
+    'decision analysis'			=> 'Decision making',
+    'decision making'			=> 'Decision making',
+    'decision processes'		=> 'Decision making',
+    'decoration of food'		=> 'Garnishes (Cooking)',
+    'decoration, interior'		=> 'Interior decoration',
+    'decoy ships'			=> 'Q-ships',
+    'decriminalization of illegal drugs' => 'Drug legalization',
+    'delivery of health care'		=> 'Medical care',
+    'delivery of medical care'		=> 'Medical care',
+    'depository institutions'		=> 'Banks and banking',
+    'deprivation, sensory'		=> 'Sensory deprivation',
+    'deprivation, sleep'		=> 'Sleep deprivation',
+    'design of logic systems'		=> 'Logic design',
+    'design, logic'			=> 'Logic design',
+    'desk calculators'			=> 'Calculators',
+    'desserts'				=> 'Desserts',
+    'destitution'			=> 'Poverty',
+    'devaluation of currency'		=> 'Monetary policy',
+    'development of application software' => 'Application software--Development',
+    'development of web sites'		=> 'Web site development',
+    'development stage businesses'	=> 'New business enterprises',
+    'development stage enterprises'	=> 'New business enterprises',
+    'development, business'		=> 'Business--Development',
+    'development, economic'		=> 'Economic development',
+    'development, sustainable'		=> 'Sustainable development',
+    'devon cooking'			=> 'Cooking, English--Devon style',
+    'devonshire cooking'		=> 'Cooking, English--Devon style',
+    'dickering'				=> 'Negotiation',
+    'diet'				=> 'Diet',
+    'diets'				=> 'Reducing diets',
+    'digital art'			=> 'Digital art',
+    'digital compact discs'		=> 'Compact discs',
+    'digital compact disks'		=> 'Compact discs',
+    'digital media'			=> 'Digital media',
+    'digital publishing'		=> 'Electronic publishing',
+    'dimensions'			=> 'Dimensions',
+    'dip systems'			=> 'Document imaging systems',
+    'direct mail campaigns'		=> 'Direct marketing',
+    'direct marketing'			=> 'Direct marketing',
+    'discoverers'			=> 'Explorers',
+    'disc world (imaginary place)'	=> 'Discworld (Imaginary place)',
+    'disc world'			=> 'Discworld (Imaginary place)',
+    'discs, compact'			=> 'Compact discs',
+    'discs, optical'			=> 'Optical disks',
+    'discworld (imaginary place)'	=> 'Discworld (Imaginary place)',
+    'discworld'				=> 'Discworld (Imaginary place)',
+    'disk operating systems'		=> 'Operating systems (Computers)',
+    'disks, compact'			=> 'Compact discs',
+    'disks, optical'			=> 'Optical disks',
+    'dispute settlement'		=> 'Conflict management',
+    'distilled beverages'		=> 'Liquors',
+    'distributed computer systems'	=> 'Electronic data processing--Distributed processing',
+    'distributed computing systems'	=> 'Electronic data processing--Distributed processing',
+    'distributed computing'		=> 'Electronic data processing--Distributed processing',
+    'distributed processing'		=> 'Electronic data processing--Distributed processing',
+    'distribution (economic theory)'	=> 'Distribution (Economic theory)',
+    'distributorship, multilevel'	=> 'Multilevel marketing',
+    'divination'			=> 'Divination',
+    'divine healing'			=> 'Spiritual healing',
+    'divine messengers'			=> 'Angels',
+    'djin'				=> 'Jinn',
+    'djinn'				=> 'Jinn',
+    'djinni'				=> 'Jinn',
+    'djinns'				=> 'Jinn',
+    'djins'				=> 'Jinn',
+    'depuy, william e. (william eugene), 1919-1992'
+      => 'DePuy, William E. (William Eugene), 1919-1992',
+    'doctrinal and controversial works'	=> 'Controversial literature',
+    'document image processing'		=> 'Document imaging systems',
+    'document imaging systems'		=> 'Document imaging systems',
+    'document management systems'	=> 'Document imaging systems',
+    'document management'		=> 'Document imaging systems',
+    'doffing'				=> 'Textile industry',
+    'dolls'				=> 'Dolls',
+    'domestic fiction'			=> 'Domestic fiction',
+    'domestic marketing'		=> 'Marketing',
+    'domestic novels'			=> 'Domestic fiction',
+    'domestic rabbit'			=> 'Rabbits',
+    'domestic rabbits'			=> 'Rabbits',
+    'double entry bookkeeping'		=> 'Bookkeeping',
+    'drawing technique'			=> 'Drawing',
+    'drawing'				=> 'Drawing',
+    'drawing--technique'		=> 'Drawing',
+    'dreaming'				=> 'Dreams',
+    'dreams'				=> 'Dreams',
+    'drinking water'			=> 'Drinking water',
+    'drinks'				=> 'Beverages',
+    'drug decriminalization'		=> 'Drug legalization',
+    'drug legalization'			=> 'Drug legalization',
+    'drugs'				=> 'Drugs',
+    'dying'				=> 'Death',
+    'e-business'			=> 'Electronic commerce',
+    'e-commerce'			=> 'Electronic commerce',
+    'e-mail correspondence'		=> 'Electronic mail messages',
+    'e-mail messages'			=> 'Electronic mail messages',
+    'e-mail software'			=> 'Electronic mail systems',
+    'e-mail systems'			=> 'Electronic mail systems',
+    'e-mail'				=> 'Electronic mail messages',
+    'e-tailing'				=> 'Electronic commerce',
+    'earthenware'			=> 'Pottery',
+    'easy and quick cooking'		=> 'Quick and easy cooking',
+    'ebusiness'				=> 'Electronic commerce',
+    'ecclesiastical corporations'	=> 'Corporations, Religious',
+    'ecological monitoring'		=> 'Environmental monitoring',
+    'ecologically sustainable development' => 'Sustainable development',
+    'ecommerce'				=> 'Electronic commerce',
+    'econometrics'			=> 'Econometrics',
+    'economic conditions'		=> 'Economic conditions',
+    'economic development'		=> 'Economic development',
+    'economic development, sustainable' => 'Sustainable development',
+    'economic espionage'		=> 'Business intelligence',
+    'economic goods'			=> 'Commercial products',
+    'economic growth'			=> 'Economic development',
+    # Works on the history of economics as a discipline are entered under Economics--History
+    'economic history'			=> 'Economic history',
+    'economic infrastructure'		=> 'Infrastructure (Economics)',
+    'economic statistics'		=> 'Economic statistics',
+    'economic sustainability'		=> 'Sustainable development',
+    'economic theory'			=> 'Economics',
+    'economics of cities'		=> 'Urban economics',
+    'economics'				=> 'Economics',
+    'economics, comparative'		=> 'Comparative economics',
+    'economics--environmental aspects'	=> 'Environmental economics',
+    'economics--history'		=> 'Economics--History',
+    'edi (electronic data interchange)' => 'Electronic data interchange',
+    'edi'				=> 'Electronic data interchange',
+    'edinburgh (scotland)'		=> 'Edinburgh (Scotland)',
+    'edinburgh, scotland'		=> 'Edinburgh (Scotland)',
+    'education software'		=> 'Computer software--Education',
+    'education'				=> 'Education',
+    'education, business'		=> 'Business education',
+    'educational software'		=> 'Computer software--Education',
+    # Note that 'Educators' is different from 'Teachers'
+    'educators'				=> 'Educators',
+    'election monitoring'		=> 'Election monitoring',
+    'election observation'		=> 'Election monitoring',
+    'elections'				=> 'Elections',
+    'electoral politics'		=> 'Elections',
+    'electronic banking'		=> 'Internet banking',
+    'electronic brains'			=> 'Artificial intelligence',
+    'electronic business data interchange' => 'Electronic data interchange',
+    'electronic business'		=> 'Electronic commerce',
+    'electronic calculating machines'	=> 'Computers',
+    'electronic calculating-machines'	=> 'Computers',
+    'electronic commerce'		=> 'Electronic commerce',
+    'electronic communication networks'	=> 'Computer networks',
+    'electronic computer programming'	=> 'Computer programming',
+    'electronic computers'		=> 'Computers',
+    'electronic data interchange'	=> 'Electronic data interchange',
+    'electronic data processing'	=> 'Data processing',
+    'electronic data processing--distributed processing' => 'Electronic data processing--Distributed processing',
+    'electronic data processing--programming'	=> 'Computer programming',
+    'electronic document imaging systems' => 'Document imaging systems',
+    'electronic document management systems' => 'Document imaging systems',
+    'electronic information services'	=> 'Online information services',
+    'electronic mail messages'		=> 'Electronic mail messages',
+    'electronic mail systems'		=> 'Electronic mail systems',
+    'electronic marketing'		=> 'Telemarketing',
+    'electronic media'			=> 'Digital media',
+    'electronic publishing'		=> 'Electronic publishing',
+    'electronic social networks'	=> 'Online social networks',
+    'electronic spread sheets'		=> 'Electronic spreadsheets',
+    'electronic spreadsheet software'	=> 'Electronic spreadsheets--Software',
+    'electronic spreadsheets'		=> 'Electronic spreadsheets',
+    'electronic spreadsheets--software' => 'Electronic spreadsheets--Software',
+    'electronic trading'		=> 'Electronic trading of securities',
+    'electronic trading of securities'	=> 'Electronic trading of securities',
+    'email correspondence'		=> 'Electronic mail messages',
+    'email messages'			=> 'Electronic mail messages',
+    'email software'			=> 'Electronic mail systems',
+    'email systems'			=> 'Electronic mail systems',
+    'email'				=> 'Electronic mail messages',
+    'employment management'		=> 'Personnel management',
+    'enchanters'			=> 'Wizards',
+    'end of life'			=> 'Death',
+    'energetic healing'			=> 'Energy medicine',
+    'energy healing'			=> 'Energy medicine',
+    'energy industries'			=> 'Energy industries',
+    'energy medicine'			=> 'Energy medicine',
+    'engeland'				=> 'England',
+    'engineering design'	        => 'Engineering design',
+    'engineering--authorship'		=> 'Technical writing',
+    'engines, search'			=> 'Search engines',
+    'england'				=> 'England',
+    'england--fiction'			=> 'England--Fiction',
+    'england--social life and customs'	=> 'England--Social life and customs',
+    'england--social life and customs--19th century--fiction' => 'England--Social life and customs--19th century--Fiction',
+    'english biscuits'			=> 'Cookies',
+    'english cooking'			=> 'Cooking, English',
+    'english customs'			=> 'England--Social life and customs',
+    'english fiction'			=> 'English fiction',
+    'english grammar'			=> 'English language--Grammar',
+    'english language'			=> 'English language',
+    'english language--grammar'		=> 'English language--Grammar',
+    'english literature'		=> 'English literature',
+    'enochian magic'			=> 'Enochian magic',
+    'enochian magick'			=> 'Enochian magic',
+    'enslavement'			=> 'Slavery',
+    'enterprises'			=> 'Business enterprises',
+    'enterprises, business'		=> 'Business enterprises',
+    'entertaining'			=> 'Entertaining',
+    # 'Entertainment industry' is not an official LC subject, but is submitted as one
+    'entertainment industry'		=> 'Entertainment industry',
+    'entheogens'			=> 'Hallucinogenic drugs',
+    'entrées (cookery)'			=> 'Entrées (Cooking)',
+    'entrées (cooking)'			=> 'Entrées (Cooking)',
+    'entrepreneur'			=> 'Entrepreneurship',
+    'entrepreneurs, women'		=> 'Businesswomen',
+    'entrepreneurship'			=> 'Entrepreneurship',
+    'environment, work'			=> 'Work environment',
+    'environmental economics'		=> 'Environmental economics',
+    'environmental monitoring'		=> 'Environmental monitoring',
+    'environmental quality'		=> 'Environmental quality',
+    'environmental quality--economic aspects' => 'Environmental economics',
+    'environmental quality--measurement' => 'Environmental monitoring',
+    'environmental quality--monitoring' => 'Environmental monitoring',
+    'environmentalists'			=> 'Environmentalists',
+    'environmentalists--biography'	=> 'Environmentalists--Biography',
+    'environments, virtual'		=> 'Virtual reality',
+    'equipment, capital'		=> 'Industrial equipment',
+    'equipment, industrial'		=> 'Industrial equipment',
+    'equities'				=> 'Stocks',
+    'equity capital'			=> 'Stocks',
+    'equity financing'			=> 'Stocks',
+    'erotic art'			=> 'Erotic art',
+    'erotic fiction'			=> 'Erotic stories',
+    'erotic stories'			=> 'Erotic stories',
+    'erotic'				=> 'Erotic stories',
+    'erotica'				=> 'Erotic stories',
+    'esd (ecologically sustainable development)' => 'Sustainable development',
+    'esme weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'esme weatherwax'			=> 'Weatherwax, Granny (Fictitious character)',
+    'esmeralda weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'esmeralda weatherwax'		=> 'Weatherwax, Granny (Fictitious character)',
+    'esp'				=> 'Extrasensory perception',
+    'espionage, business'		=> 'Business intelligence',
+    'espionage, corporate'		=> 'Business intelligence',
+    'espionage, economic'		=> 'Business intelligence',
+    'espionage, industrial'		=> 'Business intelligence',
+    'eternal life'			=> 'Immortality',
+    'etheric world intelligences'	=> 'Guides (Spiritualism)',
+    'ethics in government'		=> 'Political ethics',
+    'ethics'				=> 'Ethics',
+    'ethics, political'			=> 'Political ethics',
+    'ethics, primitive'			=> 'Ethics',
+    'ethology'				=> 'Ethics',
+    'etiquette'				=> 'Etiquette',
+    'european art'			=> 'Art, European',
+    'european cooking'			=> 'Cooking, European',
+    'evidence control'			=> 'Evidence preservation',
+    'evidence preservation'		=> 'Evidence preservation',
+    'evidence, criminal--preservation'	=> 'Evidence preservation',
+    'ex-convicts'			=> 'Ex-convicts',
+    'exchange, foreign'			=> 'Foreign exchange',
+    'exchanges, commodity'		=> 'Commodity exchanges',
+    'exchanges, produce'		=> 'Commodity exchanges',
+    'excursions'			=> 'Tourism',
+    'executive coaching'		=> 'Executive coaching',
+    'executive information systems'	=> 'Management information systems',
+    'executive power'			=> 'Executive power',
+    'executive power--united states'	=> 'Executive power--United States',
+    'exercise'				=> 'Physical fitness',
+    'expert systems (computer science)'	=> 'Expert systems (Computer science)',
+    'expert systems'			=> 'Expert systems (Computer science)',
+    'explorers'				=> 'Explorers',
+    'explorers--biography'		=> 'Explorers--Biography',
+    'export marketing'			=> 'Export marketing',
+    'exports & imports'			=> 'International trade',
+    'exports and imports'		=> 'International trade',
+    'extendible markup language'	=> 'XML (Document markup language)',
+    'extensible markup language'	=> 'XML (Document markup language)',
+    'external trade'			=> 'International trade',
+    'extranets (computer networks)'	=> 'Extranets (Computer networks)',
+    'extranets'				=> 'Extranets (Computer networks)',
+    'extrasensory perception'		=> 'Extrasensory perception',
+    'facilities management'		=> 'Facility management',
+    'facility management'		=> 'Facility management',
+    'factual data analysis'		=> 'Data mining',
+    'faerie'				=> 'Fairies',
+    'fairie'				=> 'Fairies',
+    'fairies'				=> 'Fairies',
+    'fairy tales'			=> 'Fairy tales',
+    'fairy'				=> 'Fairies',
+    'faith healing'			=> 'Spiritual healing',
+    'faith-cure'			=> 'Spiritual healing',
+    'family budgets'			=> 'Budgets, Personal',
+    'family'				=> 'Family',
+    'fantastic fiction'			=> 'Fantasy fiction',
+    'fantasy fiction'			=> 'Fantasy fiction',
+    'fantasy fiction--authorship'	=> 'Fantasy fiction--Authorship',
+    'fantasy fiction--technique'	=> 'Fantasy fiction--Technique',
+    'fantasy'				=> 'Fantasy fiction',
+    'fat-free diet'			=> 'Low-fat diet',
+    'fatness'				=> 'Obesity',
+    'fear of being alone'		=> 'Agoraphobia',
+    'fear of isolation'			=> 'Agoraphobia',
+    'fear of open space'		=> 'Agoraphobia',
+    'fear of open spaces'		=> 'Agoraphobia',
+    'federal corporation tax'		=> 'Corporations--Taxation',
+    'feng shui'				=> 'Feng shui',
+    'festive cooking'			=> 'Holiday cooking',
+    'fiction action adventure'		=> 'Adventure stories',
+    'fiction'				=> 'Fiction',
+    'fiction, juvenile'			=> 'Fiction, Juvenile',
+    'fiction, paranormal'		=> 'Paranormal fiction',
+    'fiction--fantasy'			=> 'Fantasy fiction',
+    'fiction--paranormal'		=> 'Paranormal fiction',
+    'fiction--science fiction'		=> 'Science fiction',
+    'fictitious places'			=> 'Imaginary places',
+    'figurines'				=> 'Figurines',
+    'file concatenation (statistics)'	=> 'Statistical matching',
+    'finance'				=> 'Finance',
+    'finance, personal'			=> 'Finance, Personal',
+    'finance, personal--planning'	=> 'Finance, Personal',
+    'finance, personal--software'	=> 'Finance, Personal--Software',
+    'finance, public'			=> 'Finance, Public',
+    'financial analysis of corporations' => 'Corporations--Finance',
+    'financial management of corporations' => 'Corporations--Finance',
+    'financial management, corporate'	=> 'Corporations--Finance',
+    'financial management, personal'	=> 'Finance, Personal',
+    'financial planning of corporations' => 'Corporations--Finance',
+    'financial planning, personal'	=> 'Finance, Personal',
+    'financial risk management'		=> 'Financial risk management',
+    'financial services industry'	=> 'Financial services industry',
+    'financial services'		=> 'Financial services industry',
+    'firearms'				=> 'Firearms',
+    'firms'				=> 'Business enterprises',
+    # 'First contact' is not directly a LC heading, but submitted as a recommendation
+    'first contact'			=> 'First contact',
+    'fitness, physical'			=> 'Physical fitness',
+    'flasks'				=> 'Bottles',
+    'flatboats'				=> 'Flatboats',
+    'flight'				=> 'Flight',
+    'flying saucers'			=> 'Unidentified flying objects',
+    'flying-machines'			=> 'Flying-machines',
+    'folk art'				=> 'Folk art',
+    'food allergy'			=> 'Food allergy',
+    'food calories'			=> 'Food--Caloric content',
+    'food decoration'			=> 'Garnishes (Cooking)',
+    'food industry and trade'		=> 'Food industry and trade',
+    'food preparation industry'		=> 'Food industry and trade',
+    'food preparation'			=> 'Cooking',
+    'food processing industry'		=> 'Food industry and trade',
+    'food processing'			=> 'Food industry and trade',
+    'food trade'			=> 'Food industry and trade',
+    'food, baby'			=> 'Baby foods',
+    'food, raw'				=> 'Raw foods',
+    'food--caloric content'		=> 'Food--Caloric content',
+    'food--health aspects',		=> 'Nutrition',
+    'foods, baby'			=> 'Baby foods',
+    'foods, infant'			=> 'Baby foods',
+    'foods, raw'			=> 'Raw foods',
+    'forecasting, business'		=> 'Business forecasting',
+    'foreign commerce'			=> 'International trade',
+    'foreign exchange'			=> 'Foreign exchange',
+    'foreign languages'			=> 'Language and languages',
+    'foreign trade'			=> 'International trade',
+    'foreign trade policy'		=> 'Commercial policy',
+    'forex'				=> 'Foreign exchange',
+    'fortran (computer program language)' => 'FORTRAN (Computer program language)',
+    'fortune-telling'			=> 'Fortune-telling',
+    'fortunetelling'			=> 'Fortune-telling',
+    'franchises (retail trade)'		=> 'Franchises (Retail trade)',
+    'franchises, retail'		=> 'Franchises (Retail trade)',
+    'franchises, taxation of'		=> 'Corporations--Taxation',
+    'franchises, taxation'		=> 'Corporations--Taxation',
+    'franchises--taxation'		=> 'Corporations--Taxation',
+    'free enterprise'			=> 'Free enterprise',
+    'free markets'			=> 'Free enterprise',
+    'french cooking'			=> 'Cooking, French',
+    'french-canadian cooking'		=> 'Cooking, French-Canadian',
+    'fundamental rights'		=> 'Civil rights',
+    'funding'				=> 'Finance',
+    'funnies'				=> 'Comic books, strips, etc',
+    'furniture'				=> 'Furniture',
+    'fusion of corporations'		=> 'Consolidation and merger of corporations',
+    'fusion, data (statistics)'		=> 'Statistical matching',
+    'fusion, data'			=> 'Statistical matching',
+    # 'Future life' is a separate LC Subject meaning 'life after
+    # death', not 'life in the future', but it is so confusing that it
+    # just gets folded into 'Immortality' in this code.
+    'future life'			=> 'Immortality',
+    # 'Futures' is futures trading, not potential future events
+    'futures contracts'			=> 'Futures',
+    'futures trading'			=> 'Futures',
+    'futures'				=> 'Futures',
+    'fx (finance)'			=> 'Foreign exchange',
+    'gaap (accounting)'			=> 'Accounting--Standards',
+    'gaap'				=> 'Accounting--Standards',
+    'gadgets'				=> 'Implements, utensils, etc',
+    'game programming'			=> 'Computer games--Programming',
+    'garnish, harry (fictitious character)' => 'Garnish, Harry (Fictitious character)',
+    'garnishes (cookery)'		=> 'Garnishes (Cooking)',
+    'garnishes (cooking)'		=> 'Garnishes (Cooking)',
+    'garnishes'				=> 'Garnishes (Cooking)',
+    'garnishing (cookery)'		=> 'Garnishes (Cooking)',
+    'garnishing (cooking)'		=> 'Garnishes (Cooking)',
+    'garnishing'			=> 'Garnishes (Cooking)',
+    'gascon cooking'			=> 'Cooking, French--Gascony style',
+    'gascony cooking'			=> 'Cooking, French--Gascony style',
+    'gasoline automobiles'		=> 'Automobiles',
+    'generally accepted accounting principles' => 'Accounting--Standards',
+    'generals--united states'		=> 'Generals--United States',
+    'genies'				=> 'Jinn',
+    'genii'				=> 'Jinn',
+    'german cooking'			=> 'Cooking, German',
+    'ghost stories'			=> 'Ghost stories',
+    'ghosts'				=> 'Ghost stories',
+    'glass'				=> 'Glass',
+    'glassman, josh (fictitious character)' => 'Glassman, Josh (Fictitious character)',
+    'glassman, josh'			=> 'Glassman, Josh (Fictitious character)',
+    'glassware'				=> 'Glassware',
+    'global cities'			=> 'Cities and towns',
+    'global commerce'			=> 'International trade',
+    'global corporations'		=> 'International business enterprises',
+    'global terrorism'			=> 'Terrorism',
+    'global trade'			=> 'International trade',
+    'gluten-free foods'			=> 'Gluten-free foods',
+    'goal programming'			=> 'Programming (Mathematics)',
+    'goblins'				=> 'Goblins',
+    'goddess religion'			=> 'Goddess religion',
+    'government'			=> 'Government',
+    'government companies'		=> 'Corporations, Government',
+    'government corporations'		=> 'Corporations, Government',
+    'government ethics'			=> 'Political ethics',
+    'government, resistance to'		=> 'Government, Resistance to',
+    'government, resistance to--united states' => 'Government, Resistance to--United States',
+    'graffiti'				=> 'Graffiti',
+    'graffiti artists'			=> 'Graffiti artists',
+    'graffiti culture'			=> 'Graffiti',
+    'graphic data processing'		=> 'Computer graphics',
+    'graphics, computer'		=> 'Computer graphics',
+    'granny weatherwax (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'granny weatherwax'			=> 'Weatherwax, Granny (Fictitious character)',
+    'greek cooking'			=> 'Cooking, Greek',
+    'greek mythology'			=> 'Mythology, Greek',
+    'green business'			=> 'Business enterprises--Environmental aspects',
+    'grief'				=> 'Grief',
+    'grilling (cooking)'		=> 'Barbecuing',
+    'grim reaper'			=> 'Death (Personification)',
+    'growing up'			=> 'Coming of age',
+    'guests, entertaining'		=> 'Entertaining',
+    'guides (spiritualism)'		=> 'Guides (Spiritualism)',
+    'guides, spirit'			=> 'Guides (Spiritualism)',
+    'gunning'				=> 'Shooting',
+    'guns'				=> 'Firearms',
+    'haggling'				=> 'Negotiation',
+    'hares'				=> 'Rabbits',
+    'harry garnish (fictitious character)' => 'Garnish, Harry (Fictitious character)',
+    'higgling'				=> 'Negotiation',
+    'hallucinations and illusions'	=> 'Hallucinations and illusions',
+    'hallucinogenic drugs'		=> 'Hallucinogenic drugs',
+    'hallucinogens'			=> 'Hallucinogenic drugs',
+    'hardware, computer'		=> 'Computers',
+    'heads of government'		=> 'Heads of state',
+    'heads of state'			=> 'Heads of state',
+    'heads of state--biography'		=> 'Heads of state--Biography',
+    'healing'				=> 'Healing',
+    'health & healing'			=> 'Healing',
+    'health & fitness'			=> 'Physical fitness',
+    'health and fitness'		=> 'Physical fitness',
+    'health care delivery'		=> 'Medical care',
+    'health care planning'		=> 'Health planning',
+    'health care'			=> 'Medical care',
+    'health informatics'		=> 'Medical informatics',
+    'health insurance'			=> 'Health insurance',
+    'health planning'			=> 'Health planning',
+    'health plans'			=> 'Health planning',
+    'health plans, prepaid'		=> 'Health insurance',
+    'health services planning'		=> 'Health planning',
+    'health services'			=> 'Medical care',
+    'health thoughts'			=> 'Mental healing',
+    'healthcare'			=> 'Medical care',
+    'heart--diseases--nutritional aspects'	=> 'Heart--Diseases--Nutritional aspects',
+    'heavy weapons'			=> 'Ordnance',
+    'herbal cooking'			=> 'Cooking (Herbs)',
+    'herbs'				=> 'Herbs',
+    'herbs--use in cooking'		=> 'Cooking (Herbs)',
+    'hermeticism'			=> 'Hermetism',
+    'hermetism'				=> 'Hermetism',
+    'high tech'				=> 'High technology',
+    'high technology'			=> 'High technology',
+    'hispanic american art'		=> 'Hispanic American art',
+    'historical fiction'		=> 'Historical fiction',
+    'historical fiction, chinese'	=> 'Historical fiction, Chinese',
+    'history of cooking'		=> 'Cooking--History',
+    'history'				=> 'History',
+    'history--20th century'		=> 'History--20th century',
+    'history--cooking'			=> 'Cooking--History',
+    'history, economic'			=> 'Economic history',
+    'history, military'			=> 'History, Military',
+    'holiday cookery'			=> 'Holiday cooking',
+    'holiday cooking'			=> 'Holiday cooking',
+    'holiday industry'			=> 'Tourism',
+    'home businesses'			=> 'Home-based businesses',
+    'home buying'			=> 'House buying',
+    'home computers'			=> 'Microcomputers',
+    'home decoration'			=> 'Interior decoration',
+    'home purchase'			=> 'House buying',
+    'home purchasing'			=> 'House buying',
+    'home selling'			=> 'House selling',
+    'home-based businesses'		=> 'Home-based businesses',
+    'homosexuality'			=> 'Homosexuality',
+    'horoscopes'			=> 'Horoscopes',
+    "hors d'oeuvres"			=> 'Appetizers',
+    'hostile takeovers of corporations'	=> 'Consolidation and merger of corporations',
+    'hostile takeovers'			=> 'Consolidation and merger of corporations',
+    'hot dish cooking'			=> 'Casserole cooking',
+    'hotdish cooking'			=> 'Casserole cooking',
+    'house buying'			=> 'House buying',
+    'house decoration'			=> 'Interior decoration',
+    'house guests, entertaining'	=> 'Entertaining',
+    'house hunting'			=> 'House buying',
+    'house selling'			=> 'House selling',
+    'houseguests, entertaining'		=> 'Entertaining',
+    'household utensils'		=> 'Implements, utensils, etc',
+    'how to start a business'		=> 'New business enterprises',
+    'how to start businesses'		=> 'New business enterprises',
+    'horror'				=> 'Horror',
+    'html (document markup language)'	=> 'HTML (Document markup language)',
+    'html'				=> 'HTML (Document markup language)',
+    'http cookies (computer science)'	=> 'Cookies (Computer science)',
+    'http cookies'			=> 'Cookies (Computer science)',
+    # 'Human beings in art' is for representations of humans in art
+    'human beings in art'		=> 'Human beings in art',
+    'human body in art'			=> 'Human figure in art',
+    'human body--care and hygiene'	=> 'Hygiene',
+    'human factors in computing systems' => 'Human-computer interaction',
+    'human females'			=> 'Women',
+    # 'Human figure in art' is for art technique, not representations
+    'human figure in art'		=> 'Human figure in art',
+    'human resource management'		=> 'Personnel management',
+    'human resources management'	=> 'Personnel management',
+    'human rights'			=> 'Human rights',
+    'human sexuality'			=> 'Sex',
+    'human-computer interaction'	=> 'Human-computer interaction',
+    'humans in art'			=> 'Human beings in art',
+    'humanx'		 		=> 'Humanx Commonwealth (Imaginary organization)',
+    'humanx commonwealth' 		=> 'Humanx Commonwealth (Imaginary organization)',
+    'humanx commonwealth (imaginary organization)'
+      => 'Humanx Commonwealth (Imaginary organization)',
+    'humanx commonwealth (imaginary organization)--fiction'
+      => 'Humanx Commonwealth (Imaginary organization)',
+    'humor'				=> 'Humor',
+    'humor adult humor and comedy'	=> 'Humor',
+    'humour'				=> 'Humor',
+    'hygiene'				=> 'Hygiene',
+    'hygiene, public'			=> 'Public health',
+    'hygiene, social'			=> 'Public health',
+    'hypertext markup language'		=> 'HTML (Document markup language)',
+    'icebreakers (ships)'		=> 'Icebreakers (Ships)',
+    'illiteracy'			=> 'Literacy',
+    'illusions'				=> 'Hallucinations and illusions',
+    'illusions, optical'		=> 'Optical illusions',
+    'image processing'			=> 'Image processing',
+    'imaginary cities'			=> 'Imaginary places',
+    'imaginary islands'			=> 'Imaginary places',
+    'imaginary places'			=> 'Imaginary places',
+    # 'Immortalism' refers to living indefinitely in the flesh
+    'immortalism'			=> 'Immortalism',
+    # 'Immortality' refers to survival of the soul after death
+    'immortality'			=> 'Immortality',
+    'implements, utensils, etc'		=> 'Implements, utensils, etc',
+    'imports & exports'			=> 'International trade',
+    'imports and exports'		=> 'International trade',
+    'imputation, mass (statistics)'	=> 'Statistical matching',
+    'income tax'			=> 'Income tax',
+    'indemnity insurance'		=> 'Insurance',
+    'industrial administration'		=> 'Industrial management',
+    'industrial communication'		=> 'Business communication',
+    'industrial equipment'		=> 'Industrial equipment',
+    'industrial equipment--marketing'	=> 'Industrial marketing',
+    'industrial espionage'		=> 'Business intelligence',
+    'industrial management'		=> 'Industrial management',
+    'industrial marketing'		=> 'Industrial marketing',
+    'industrial policy'			=> 'Industrial policy',
+    'industrial production'		=> 'Industries',
+    'industrial project management'	=> 'Project management',
+    'industries'			=> 'Industries',
+    'industries--equipment and supplies' => 'Industrial equipment',
+    'industries--government policy'	=> 'Industrial policy',
+    'industries--public relations'	=> 'Public relations',
+    'industry and state'		=> 'Industrial policy',
+    'industry'				=> 'Industries',
+    'infancy'				=> 'Infants',
+    'infant foods'			=> 'Baby foods',
+    'infants'				=> 'Infants',
+    'infants--food'			=> 'Baby foods',
+    'inflation (finance)'		=> 'Inflation (Finance)',
+    'inflation'				=> 'Inflation (Finance)',
+    'informatics'			=> 'Computer science',
+    'information resources management'	=> 'Information resources management',
+    'information systems management'	=> 'Information resources management',
+    'information technology'		=> 'Information technology',
+    'information theory'		=> 'Information theory',
+    'information warehousing'		=> 'Data warehousing',
+    'infrastructure (economics)'	=> 'Infrastructure (Economics)',
+    'inghilterra'			=> 'England',
+    'inglaterra'			=> 'England',
+    'ink drawing'			=> 'Pen drawing',
+    'inspiration'			=> 'Inspiration',
+    'inspiration--religious aspects'	=> 'Inspiration--Religious aspects',
+    'instruction'			=> 'Education',
+    'instructors'			=> 'Teachers',
+    'instruments, musical'		=> 'Musical instruments',
+    'insurance coverage'		=> 'Insurance',
+    'insurance industry'		=> 'Insurance',
+    'insurance protection'		=> 'Insurance',
+    'insurance risk'			=> 'Risk (Insurance)',
+    'insurance'				=> 'Insurance',
+    'insurance, automobile'		=> 'Automobile insurance',
+    'insurance, casualty'		=> 'Casualty insurance',
+    'insurance, health'			=> 'Health insurance',
+    'insurance, liability'		=> 'Liability insurance',
+    'insurance, life'			=> 'Life insurance',
+    'insurance, property'		=> 'Property insurance',
+    'insurance--risk'			=> 'Risk (Insurance)',
+    'intellectronics'			=> 'Artificial intelligence',
+    'intellectual capital'		=> 'Intellectual capital',
+    'intelligence, artificial'		=> 'Artificial intelligence',
+    'intelligence, business'		=> 'Business intelligence',
+    'intelligence, corporate'		=> 'Business intelligence',
+    'intelligences, etheric world'	=> 'Guides (Spiritualism)',
+    'intelligent machines'		=> 'Artificial intelligence',
+    'interaction, human-computer'	=> 'Human-computer interaction',
+    'interactive computer systems'	=> 'Interactive computer systems',
+    'intercourse, sexual'		=> 'Sex',
+    'interest and usury'		=> 'Interest',
+    'interest'				=> 'Interest',
+    'interfaces, user (computer systems)' => 'User interfaces (Computer systems)',
+    'interfaces, user'			=> 'User interfaces (Computer systems)',
+    'interior decoration'		=> 'Interior decoration',
+    'interior design'			=> 'Interior decoration',
+    'internal internets (computer networks)' => 'Intranets (Computer networks)',
+    'internal internets'		=> 'Intranets (Computer networks)',
+    'international accounting'		=> 'International business enterprises--Accounting',
+    'international business enterprises' => 'International business enterprises',
+    'international business enterprises--accounting' =>
+      'International business enterprises--Accounting',
+    'international business enterprises--taxation' =>
+      'International business enterprises--Taxation',
+    'international competition'		=> 'Competition, International',
+    'international corporations'	=> 'International business enterprises',
+    'international economics'		=> 'Competition, International',
+    'international exchange'		=> 'Foreign exchange',
+    'international marketing'		=> 'Export marketing',
+    'international taxation'		=> 'International business enterprises--Taxation',
+    'international taxes'		=> 'International business enterprises--Taxation',
+    'international terrorism'		=> 'Terrorism',
+    'international trade policy'	=> 'Commercial policy',
+    'international trade'		=> 'International trade',
+    'internet advertising'		=> 'Internet advertising',
+    'internet application development'	=> 'Application software--Internet--Development',
+    'internet application software'	=> 'Application software--Internet',
+    'internet applications'		=> 'Application software--Internet',
+    'internet apps'			=> 'Application software--Internet',
+    'internet auctions'			=> 'Internet auctions',
+    'internet banking'			=> 'Internet banking',
+    'internet bookstores'		=> 'Internet bookstores',
+    'internet broadcasting'		=> 'Webcasting',
+    'internet browsers'			=> 'Browsers (Computer programs)',
+    'internet commerce'			=> 'Electronic commerce',
+    'internet drugstores'		=> 'Internet pharmacies',
+    'internet marketing'		=> 'Internet advertising',
+    'internet pharmacies'		=> 'Internet pharmacies',
+    'internet retailing'		=> 'Electronic commerce',
+    'internet'				=> 'Internet',
+    'internet--safety measures'		=> 'Internet--Safety measures',
+    'internship programs'		=> 'Internship programs',
+    'internships'			=> 'Internship programs',
+    'intranets (computer networks)'	=> 'Intranets (Computer networks)',
+    'intranets'				=> 'Intranets (Computer networks)',
+    'intrapreneur'			=> 'Entrepreneurship',
+    'investing'				=> 'Investments',
+    'investment companies'		=> 'Mutual funds',
+    'investment in real estate'		=> 'Real estate investment',
+    'investment management'		=> 'Investments',
+    'investment options'		=> 'Options (Finance)',
+    'investment trusts'			=> 'Mutual funds',
+    'investments'			=> 'Investments',
+    'irrationalism (philosophy)'	=> 'Irrationalism (Philosophy)',
+    'irrationalism'			=> 'Irrationalism (Philosophy)',
+    'irrationalist (philosophy)'	=> 'Irrationalism (Philosophy)',
+    'irrationalist'			=> 'Irrationalism (Philosophy)',
+    'islands, imaginary'		=> 'Imaginary places',
+    'isolation'				=> 'Social isolation',
+    'isolation, fear of'		=> 'Agoraphobia',
+    'isolation, perceptual'		=> 'Sensory deprivation',
+    'it (information technology)'	=> 'Information technology',
+    'jackrabbits'			=> 'Rabbits',
+    'jane austen'			=> 'Austen, Jane, 1775-1817',
+    'jane austen, 1775-1817'		=> 'Austen, Jane, 1775-1817',
+    'java (computer program language)'	=> 'Java (Computer program language)',
+    'javascript (computer program language)' => 'JavaScript (Computer program language)',
+    'javascript'			=> 'JavaScript (Computer program language)',
+    'jewellery'				=> 'Jewelry',
+    'jewelry'				=> 'Jewelry',
+    'jewels'				=> 'Jewelry',
+    'jinn'				=> 'Jinn',
+    'jinni'				=> 'Jinn',
+    'jinns'				=> 'Jinn',
+    'job hunting'			=> 'Job hunting',
+    'job résumés'			=> 'Résumés (Employment)',
+    'job searching'			=> 'Job hunting',
+    'joblessness'			=> 'Unemployment',
+    'jobs'				=> 'Occupations',
+    'josh glassman'			=> 'Glassman, Josh (Fictitious character)',
+    'kate daniels (fictitious character)' => 'Daniels, Kate (Fictitious character)',
+    'kate daniels'			=> 'Daniels, Kate (Fictitious character)',
+    'kdd (information retrieval)'	=> 'Data mining',
+    'kdd'				=> 'Data mining',
+    'ketogenic diet'			=> 'Ketogenic diet',
+    'keyboarding'			=> 'Keyboarding',
+    'kids'				=> 'Children',
+    'kitchen utensils'			=> 'Kitchen utensils',
+    'kitchenware'			=> 'Kitchen utensils',
+    'kittens'			        => 'Kittens',
+    'knowledge capital'			=> 'Intellectual capital',
+    'knowledge discovery in data'	=> 'Data mining',
+    'knowledge discovery in databases'	=> 'Data mining',
+    'knowledge-based systems'		=> 'Expert systems (Computer science)',
+    'korean war'			=> 'Korean War, 1950-1953',
+    'korean war, 1950-1953'		=> 'Korean War, 1950-1953',
+    'labor and laboring classes'	=> 'Labor',
+    'labor'				=> 'Labor',
+    'laissez faire'			=> 'Free enterprise',
+    'laissez-faire'			=> 'Free enterprise',
+    'latin american art'		=> 'Art, Latin American',
+    'landscapes'			=> 'Landscapes',
+    'landscapes in art'			=> 'Landscapes in art',
+    'language and languages'		=> 'Language and languages',
+    'language and languages--data processing' => 'Computational linguistics',
+    'language art'			=> 'Conceptual art',
+    'language art (fine arts)'		=> 'Conceptual art',
+    'language data processing'		=> 'Computational linguistics',
+    'languages'				=> 'Language and languages',
+    'lans (computer networks)'		=> 'Local area networks (Computer networks)',
+    'lans'				=> 'Local area networks (Computer networks)',
+    'laser discs'			=> 'Optical disks',
+    'laser disks'			=> 'Optical disks',
+    'laserdiscs'			=> 'Optical disks',
+    'laserdisks'			=> 'Optical disks',
+    'later life (human life cycle)'	=> 'Old age',
+    'latin american cooking'		=> 'Cooking, Latin American',
+    'law'				=> 'Law',
+    'law, business'			=> 'Commercial law',
+    'law, commercial'			=> 'Commercial law',
+    'laws (statutes)'			=> 'Statutes',
+    'leadership coaching'		=> 'Executive coaching',
+    'leadership'			=> 'Leadership',
+    'legalization of illegal drugs'	=> 'Drug legalization',
+    'legislative acts'			=> 'Statutes',
+    'legislative enactments'		=> 'Statutes',
+    'letter writing'			=> 'Letter writing',
+    'letters'				=> 'Letters',
+    'liability insurance'		=> 'Liability insurance',
+    'liability insurance, automobile'	=> 'Automobile insurance',
+    'life'				=> 'Life',
+    'life after death'			=> 'Immortality',
+    'life drawing'			=> 'Human figure in art',
+    'life insurance'			=> 'Life insurance',
+    'life on other planets'		=> 'Life on other planets',
+    'linguistic science'		=> 'Linguistics',
+    'linguistics'			=> 'Linguistics',
+    'linguistics--data processing'	=> 'Computational linguistics',
+    'liquors'				=> 'Liquors',
+    'liquors--use in cooking'		=> 'Cooking (Liquors)',
+    'lisp (computer program language)'	=> 'LISP (Computer program language)',
+    'list processing computer language'	=> 'LISP (Computer program language)',
+    'listed options'			=> 'Options (Finance)',
+    'literacy'				=> 'Literacy',
+    'literacy, computer'		=> 'Computer literacy',
+    'literary criticism'		=> 'Criticism',
+    'literature/poetry'			=> 'Poetry',
+    'local area computer networks'	=> 'Local area networks (Computer networks)',
+    'local area networks (computer networks)'	=> 'Local area networks (Computer networks)',
+    'local area networks'		=> 'Local area networks (Computer networks)',
+    'logic design'			=> 'Logic design',
+    'london'				=> 'City of London (England)',
+    'london, england'			=> 'City of London (England)',
+    'look-ism'				=> 'Physical-appearance-based bias',
+    'lookism'				=> 'Physical-appearance-based bias',
+    'looks-ism'				=> 'Physical-appearance-based bias',
+    'looksism'				=> 'Physical-appearance-based bias',
+    'lorraine cooking'			=> 'Cooking, French--Lorraine style',
+    'losing weight'			=> 'Weight loss',
+    'loss of weight'			=> 'Weight loss',
+    'love stories'			=> 'Love stories',
+    'lovemaking'			=> 'Sex',
+    'low salt diet'			=> 'Salt-free diet',
+    'low sodium diet'			=> 'Salt-free diet',
+    'low-carb diet'			=> 'Low-carbohydrate diet',
+    'low-carbohydrate diet'		=> 'Low-carbohydrate diet',
+    'low-cholesterol diet'		=> 'Low-cholesterol diet',
+    'low-fat diet'			=> 'Low-fat diet',
+    'low-salt diet'			=> 'Salt-free diet',
+    'low-sodium diet'			=> 'Salt-free diet',
+    'machine intelligence'		=> 'Artificial intelligence',
+    'machine systems, virtual'		=> 'Virtual computer systems',
+    'machine theory'			=> 'Machine theory',
+    'machine vision'			=> 'Computer vision',
+    'mac clones'			=> 'Macintosh-compatible computers',
+    'mac computers'			=> 'Macintosh-compatible computers',
+    'machine language'			=> 'Programming languages (Electronic computers)',
+    'machine languages'			=> 'Programming languages (Electronic computers)',
+    'macintosh clones'			=> 'Macintosh-compatible computers',
+    'macintosh compatibles (computers)' => 'Macintosh-compatible computers',
+    'macintosh compatibles'		=> 'Macintosh-compatible computers',
+    'macintosh-compatible computers'	=> 'Macintosh-compatible computers',
+    'macintosh computers'		=> 'Macintosh-compatible computers',
+    'macroeconomics'			=> 'Macroeconomics',
+    'magazines'				=> 'Periodicals',
+    'magic cookies (computer science)'	=> 'Cookies (Computer science)',
+    'magic'				=> 'Magic',
+    'magic--fiction'			=> 'Magic',
+    'magicians'				=> 'Magicians',
+    'magicians--fiction'		=> 'Magicians--Fiction',
+    'mail-order business'		=> 'Mail-order business',
+    'mail-order houses'			=> 'Mail-order business',
+    'main courses (cooking)'		=> 'Entrées (Cooking)',
+    'main dishes (cooking)'		=> 'Entrées (Cooking)',
+    'making bread'			=> 'Cooking (Bread)',
+    'making decisions'			=> 'Decision making',
+    'malevolent software'		=> 'Malware (Computer software)',
+    'malicious computer code'		=> 'Malware (Computer software)',
+    'malicious software'		=> 'Malware (Computer software)',
+    'malignancy (cancer)'		=> 'Cancer',
+    'malignant tumors'			=> 'Cancer',
+    'maltreatment of children'		=> 'Child abuse',
+    'malware (computer software)'	=> 'Malware (Computer software)',
+    'malware'				=> 'Malware (Computer software)',
+    'management coaching'		=> 'Executive coaching',
+    'management decisions'		=> 'Decision making',
+    'management information systems'	=> 'Management information systems',
+    'management of computer memory'	=> 'Memory management (Computer science)',
+    'management of conflict'		=> 'Conflict management',
+    'management science'		=> 'Management science',
+    'management'			=> 'Management',
+    'management, industrial'		=> 'Industrial management',
+    'management, sales'			=> 'Sales management',
+    'managing conflict'			=> 'Conflict management',
+    'manga'				=> 'Comic books, strips, etc',
+    'manhua'				=> 'Comic books, strips, etc',
+    'manhwa'				=> 'Comic books, strips, etc',
+    'manpower utilization'		=> 'Personnel management',
+    'manufacturing industries'		=> 'Manufacturing industries',
+    'manufacturing management'		=> 'Production management',
+    'market research'			=> 'Marketing research',
+    'marketing research'		=> 'Marketing research',
+    'marketing'				=> 'Marketing',
+    'marketing, multilevel'		=> 'Multilevel marketing',
+    'marketing--research'		=> 'Marketing research',
+    'markets'				=> 'Markets',
+    'markets, free'			=> 'Free enterprise',
+    'markets--research'			=> 'Marketing research',
+    'marksmanship'			=> 'Shooting',
+    'marshall, george c. (george catlett), 1880-1959'
+      => 'Marshall, George C. (George Catlett), 1880-1959',
+    'mass communication'		=> 'Communication and traffic',
+    'mass culture'			=> 'Popular culture',
+    'mass imputation'			=> 'Statistical matching',
+    'massacres'				=> 'Massacres',
+    'massacres--vietnam'		=> 'Massacres--Vietnam',
+    'matching, data (statistics)'	=> 'Statistical matching',
+    'matching, statistical'		=> 'Statistical matching',
+    'math'				=> 'Mathematics',
+    'mathematical machine theory'	=> 'Machine theory',
+    'mathematical machines'		=> 'Calculators',
+    'mathematical programming'		=> 'Programming (Mathematics)',
+    'mathematics'			=> 'Mathematics',
+    'mechanical computers'		=> 'Calculators',
+    'medical and health care industry'	=> 'Medical care',
+    'medical and healthcare industry'	=> 'Medical care',
+    'medical care planning'		=> 'Health planning',
+    'medical care'			=> 'Medical care',
+    'medical care, prepaid'		=> 'Health insurance',
+    'medical care--planning'		=> 'Health planning',
+    'medical care--social aspects'	=> 'Social medicine',
+    'medical informatics'		=> 'Medical informatics',
+    'medical information science'	=> 'Medical informatics',
+    'medical insurance'			=> 'Health insurance',
+    'medical profession'		=> 'Medicine',
+    'medical services'			=> 'Medical care',
+    'medical sociology'			=> 'Social medicine',
+    'medical'				=> 'Medical care',
+    'medicaments'			=> 'Drugs',
+    'medications'			=> 'Drugs',
+    'medications'			=> 'Drugs',
+    'medicine (drugs)'			=> 'Drugs',
+    'medicine'				=> 'Medicine',
+    'medicine, social'			=> 'Social medicine',
+    'medicine--social aspects'		=> 'Social medicine',
+    'medicines (drugs)'			=> 'Drugs',
+    'medicines'				=> 'Drugs',
+    'meditation'			=> 'Meditation',
+    'medium-sized business'		=> 'Small business',
+    'medium-sized businesses'		=> 'Small business',
+    'memorabilia'			=> 'Collectibles',
+    'memory management (computer science)' => 'Memory management (Computer science)',
+    'memory management'			=> 'Memory management (Computer science)',
+    'mental healing'			=> 'Mental healing',
+    'mental prayer'			=> 'Meditation',
+    'mental telepathy'			=> 'Telepathy',
+    'mercantile law'			=> 'Commercial law',
+    'merchandise'			=> 'Commercial products',
+    'merger of corporations'		=> 'Consolidation and merger of corporations',
+    'mergers & acquisitions'		=> 'Consolidation and merger of corporations',
+    'mergers and acquisitions'		=> 'Consolidation and merger of corporations',
+    'mergers and acquisitions of corporations' => 'Consolidation and merger of corporations',
+    'mergers of corporations'		=> 'Consolidation and merger of corporations',
+    'mergers, corporate'		=> 'Consolidation and merger of corporations',
+    'merging, data (Statistics)'	=> 'Statistical matching',
+    'merging, data'			=> 'Statistical matching',
+    'metamorphosis--folklore'		=> 'Shapeshifting',
+    'micro computers'			=> 'Microcomputers',
+    'micro-businesses'			=> 'Small business',
+    'micro-enterprises'			=> 'Small business',
+    'microbusinesses'			=> 'Small business',
+    'microcomputers'			=> 'Microcomputers',
+    'microeconomics'			=> 'Microeconomics',
+    'microenterprises'			=> 'Small business',
+    'microprocessors'			=> 'Microprocessors',
+    'micros (microcomputers)'		=> 'Microcomputers',
+    'microsimulation modeling (statistics)' => 'Statistical matching',
+    'microwave cookery'			=> 'Microwave cooking',
+    'microwave cooking'			=> 'Microwave cooking',
+    'mid-atlantic states'		=> 'Middle Atlantic States',
+    'middle atlantic region'		=> 'Middle Atlantic States',
+    'middle atlantic states'		=> 'Middle Atlantic States',
+    'middle colonies'			=> 'Middle Atlantic States',
+    'middle states'			=> 'Middle Atlantic States',
+    'middle-atlantic states'		=> 'Middle Atlantic States',
+    'midwestern cooking'		=> 'Cooking, American--Midwestern style',
+    'military biography'		=> 'Military biography',
+    'military history'			=> 'History, Military',
+    'mind reading'			=> 'Telepathy',
+    'mind-cure'				=> 'Mental healing',
+    'mind-distorting drugs'		=> 'Hallucinogenic drugs',
+    'mind-reading'			=> 'Telepathy',
+    'mindreading'			=> 'Telepathy',
+    'mining data'			=> 'Data mining',
+    'mining, data'			=> 'Data mining',
+    'mixed media (art)'			=> 'Mixed media (Art)',
+    'mixed media crafts'		=> 'Mixed media (Art)',
+    'mixology'				=> 'Bartending',
+    'mobile computing'			=> 'Mobile computing',
+    'modeling, computer'		=> 'Computer simulation',
+    'modeling, microsimulation (statistics)' => 'Statistical matching',
+    'models, computer'			=> 'Computer simulation',
+    'modems'				=> 'Modems',
+    'modern & contemporary fiction (post c 1945)' => 'Fiction--1945-',
+    'modern art'			=> 'Art, Modern',
+    'modula-2 (computer program language)' => 'Modula-2 (Computer program language)',
+    'modula-2'				=> 'Modula-2 (Computer program language)',
+    'molesting children'		=> 'Child sexual abuse',
+    'molesting of children'		=> 'Child sexual abuse',
+    'monetary management'		=> 'Monetary policy',
+    'monetary policy'			=> 'Monetary policy',
+    'money'				=> 'Money',
+    'monitoring, election'		=> 'Election monitoring',
+    'mongolians'			=> 'Mongols',
+    'mongols'				=> 'Mongols',
+    'mongols--fiction'			=> 'Mongols--Fiction',
+    'mongols--history'			=> 'Mongols--History',
+    'monitoring, environmental'		=> 'Environmental monitoring',
+    'moral philosophy'			=> 'Ethics',
+    'morality'				=> 'Ethics',
+    'morals'				=> 'Ethics',
+    'mortgages'				=> 'Mortgages',
+    'motivational speakers'		=> 'Motivational speakers',
+    'motorcars (automobiles)'		=> 'Automobiles',
+    'motorcars'				=> 'Automobiles',
+    'mount sneffels wilderness (colo.)' => 'Mount Sneffels Wilderness (Colo.)',
+    'mourning'				=> 'Grief',
+    'mui tsai'				=> 'Slavery',
+    'multilevel distributorship'	=> 'Multilevel marketing',
+    'multilevel marketing'		=> 'Multilevel marketing',
+    'multilevel sales companies'	=> 'Multilevel marketing',
+    'multinational corporations'	=> 'International business enterprises',
+    'multinational enterprises'		=> 'International business enterprises',
+    'municipalities'			=> 'Cities and towns',
+    'murder'				=> 'Murder',
+    'murder mystery'			=> 'Mystery and detective stories',
+    'museology'				=> 'Museum techniques',
+    'museum administration'		=> 'Museum techniques',
+    'museum techniques'			=> 'Museum techniques',
+    'museums'				=> 'Museums',
+    'museums--technique'		=> 'Museum techniques',
+    'musical instruments'		=> 'Musical instruments',
+    'mutual funds'			=> 'Mutual funds',
+    'mutual insurance'			=> 'Insurance',
+    'mystery and detective stories'	=> 'Mystery and detective stories',
+    'mystery'				=> 'Mystery and detective stories',
+    'mystical theology'			=> 'Mysticism',
+    'mysticism'				=> 'Mysticism',
+    'mythology, greek'			=> 'Mythology, Greek',
+    'national resources'		=> 'Natural resources',
+    'natural language processing'	=> 'Computational linguistics',
+    'natural resources'			=> 'Natural resources',
+    'natural science'			=> 'Science',
+    'naval tactics'			=> 'Naval tactics',
+    'navigators'			=> 'Explorers',
+    'near-death experiences'		=> 'Near-death experiences',
+    'negotiating'			=> 'Negotiation',
+    'negotiation'			=> 'Negotiation',
+    'negotiations'			=> 'Negotiation',
+    'neglect of children'		=> 'Child abuse',
+    'negro art'				=> 'African American art',
+    'netcasting'			=> 'Webcasting',
+    'nets, neural (computer science)'	=> 'Neural networks (Computer science)',
+    'nets, neural (neurobiology)'	=> 'Neural networks (Neurobiology)',
+    'network marketing'			=> 'Multilevel marketing',
+    'network security'			=> 'Computer networks--Security measures',
+    'network security, computer'	=> 'Computer networks--Security measures',
+    'networks, computer'		=> 'Computer networks',
+    'networks, neural (computer science)' => 'Neural networks (Computer science)',
+    'networks, neural (neurobiology)'	=> 'Neural networks (Neurobiology)',
+    'neural nets (computer science)'	=> 'Neural networks (Computer science)',
+    'neural nets (neurobiology)'	=> 'Neural networks (Neurobiology)',
+    'neural networks (computer science)' => 'Neural networks (Computer science)',
+    'neural networks (neurobiology)'	=> 'Neural networks (Neurobiology)',
+    'new business enterprises'		=> 'New business enterprises',
+    'new companies'			=> 'New business enterprises',
+    'new england cooking'		=> 'Cooking, American--New England style',
+    'new thought'			=> 'New Thought',
+    'newspapers'			=> 'Newspapers',
+    'non-alcoholic beverages'		=> 'Non-alcoholic beverages',
+    'non-alcoholic drinks'		=> 'Non-alcoholic beverages',
+    'non-profit organizations'		=> 'Nonprofit organizations',
+    'non-profit sector'			=> 'Nonprofit organizations',
+    'non-profits'			=> 'Nonprofit organizations',
+    'non-resistance to government'	=> 'Government, Resistance to',
+    'nonprofit organizations'		=> 'Nonprofit organizations',
+    'nonprofit sector'			=> 'Nonprofit organizations',
+    'nonprofits'			=> 'Nonprofit organizations',
+    'norman cooking'			=> 'Cooking, French--Normandy style',
+    'normandy cooking'			=> 'Cooking, French--Normandy style',
+    'northwestern states'		=> 'Northwestern States',
+    'northwestern united states'	=> 'Northwestern States',
+    'not-for-profit organizations'	=> 'Nonprofit organizations',
+    'npos'				=> 'Nonprofit organizations',
+    'numerology'			=> 'Numerology',
+    'nutrition',			=> 'Nutrition',
+    'nutrition--health aspects',	=> 'Nutrition',
+    'oacet (imaginary organization)'	=> 'OACET (Imaginary organization)',
+    'oacet'		 		=> 'OACET (Imaginary organization)',
+    'obe (parapsychology)'		=> 'Astral projection',
+    'obesity'				=> 'Obesity',
+    'obesity--control'			=> 'Weight loss',
+    'obesity--etiology'			=> 'Obesity--Etiology',
+    'object-oriented programming (computer science)' => 'Object-oriented programming (Computer science)',
+    'object-oriented programming'	=> 'Object-oriented programming (Computer science)',
+    'objective c'			=> 'Objective-C (Computer program language)',
+    'objective-c (Computer program language)'	=> 'Objective-C (Computer program language)',
+    'objective-c'			=> 'Objective-C (Computer program language)',
+    'objects, art'			=> 'Art objects',
+    "objets d'art"			=> 'Art objects',
+    'occultism'				=> 'Occultism',
+    'occupations'			=> 'Occupations',
+    'office administration'		=> 'Office management',
+    'office automation'			=> 'Office practice--Automation',
+    'office equipment and supplies'	=> 'Office equipment and supplies',
+    'office equipment'			=> 'Office equipment and supplies',
+    'office etiquette'			=> 'Business etiquette',
+    'office machines'			=> 'Office equipment and supplies',
+    'office management'			=> 'Office management',
+    'office practice--automation'	=> 'Office practice--Automation',
+    'office products'			=> 'Office equipment and supplies',
+    'office supplies'			=> 'Office equipment and supplies',
+    'office systems'			=> 'Office equipment and supplies',
+    'oil painting'			=> 'Painting',
+    'old age'				=> 'Old age',
+    'on-line information services'	=> 'Online information services',
+    'online auctions'			=> 'Internet auctions',
+    'online commerce'			=> 'Electronic commerce',
+    'online information services'	=> 'Online information services',
+    'online investing'			=> 'Electronic trading of securities',
+    'online publishing'			=> 'Electronic publishing',
+    'online services (information services)'	=> 'Online information services',
+    'online social networks'		=> 'Online social networks',
+    'online trading'			=> 'Electronic trading of securities',
+    'online trading of securities'	=> 'Electronic trading of securities',
+    'open spaces'			=> 'Open spaces',
+    'open spaces, fear of'		=> 'Agoraphobia',
+    'open-end mutual funds'		=> 'Mutual funds',
+    'operational analysis'		=> 'Operations research',
+    'operational research'		=> 'Operations research',
+    'operations management'		=> 'Production management',
+    'operations research'		=> 'Operations research',
+    'operating systems (computers)'	=> 'Operating systems (Computers)',
+    'operating systems'			=> 'Operating systems (Computers)',
+    'operators, tour (industry)'	=> 'Tourism',
+    'operators, tour'			=> 'Tourism',
+    'optical computing'			=> 'Optical data processing',
+    'optical data processing'		=> 'Optical data processing',
+    'optical discs'			=> 'Optical disks',
+    'optical disks'			=> 'Optical disks',
+    'optical illusions'			=> 'Optical illusions',
+    'options (finance)'			=> 'Options (Finance)',
+    'options exchange'			=> 'Options (Finance)',
+    'options market'			=> 'Options (Finance)',
+    'options trading'			=> 'Options (Finance)',
+    'ordnance'				=> 'Ordnance',
+    'organization development'		=> 'Organizational change',
+    'organizational behavior'		=> 'Organizational behavior',
+    'organizational change'		=> 'Organizational change',
+    'organizational development'	=> 'Organizational change',
+    'organizational innovation'		=> 'Organizational change',
+    'organizations, business'		=> 'Business enterprises',
+    'organizations, nonprofit'		=> 'Nonprofit organizations',
+    'oriental art'			=> 'Art, Asian',
+    'oriental cooking'			=> 'Cooking, Asian',
+    'out-of-body experiences'		=> 'Astral projection',
+    'outdoor cookery'			=> 'Outdoor cooking',
+    'outdoor cooking'			=> 'Outdoor cooking',
+    'outsourcing'			=> 'Contracting out',
+    'overseas marketing'		=> 'Export marketing',
+    'ownership of slaves'		=> 'Slavery',
+    'pad computers'			=> 'Tablet computers',
+    'pages, web'			=> 'Web sites',
+    'painting'				=> 'Painting',
+    'painting, primitive'		=> 'Painting',
+    'painting--technique'		=> 'Painting--Technique',
+    'paintings'				=> 'Painting',
+    'palm reading'			=> 'Palmistry',
+    'palmistry'				=> 'Palmistry',
+    'palmreading'			=> 'Palmistry',
+    'papooses'				=> 'Infants',
+    'parallel programming (computer science)' => 'Parallel programming (Computer science)',
+    'parallel programming'		=> 'Parallel programming (Computer science)',
+    'paranormal'			=> 'Paranormal fiction',
+    'paranormal fiction'		=> 'Paranormal fiction',
+    'paranormal phenomena'		=> 'Parapsychology',
+    'paraphernalia, political'		=> 'Political collectibles',
+    'parapsychology'			=> 'Parapsychology',
+    'park administration'		=> 'Parks--Management',
+    'park management'			=> 'Parks--Management',
+    'parklands'				=> 'Parks',
+    'parks'				=> 'Parks',
+    'parks--management'			=> 'Parks--Management',
+    'parody'				=> 'Parody',
+    'pascal (computer program language)' => 'Pascal (Computer program language)',
+    'pastel drawing'			=> 'Pastel drawing',
+    'pastel drawing--technique'		=> 'Pastel drawing--Technique',
+    'pastel painting'			=> 'Pastel drawing',
+    'pastels'				=> 'Pastel drawing',
+    'pastry'				=> 'Pastry',
+    'pathfinder (game)'			=> 'Pathfinder (Game)',
+    'pathfinder game'			=> 'Pathfinder (Game)',
+    'pathfinder novel'			=> 'Pathfinder (Game)',
+    'pathfinder roleplaying game'	=> 'Pathfinder (Game)',
+    'pathfinder story'			=> 'Pathfinder (Game)',
+    'pathologically eclectic rubbish lister'	=> 'Perl (Computer program language)',
+    'pattern classification systems'	=> 'Pattern recognition systems',
+    'pattern recognition computers'	=> 'Pattern recognition systems',
+    'pattern recognition systems'	=> 'Pattern recognition systems',
+    'pcs (microcomputers)'		=> 'Microcomputers',
+    'pcs'				=> 'Microcomputers',
+    'peasant art'			=> 'Folk art',
+    'pedagogy'				=> 'Education',
+    'pedology (child study)'		=> 'Children',
+    'pen drawing'			=> 'Pen drawing',
+    'pen drawing--technique'		=> 'Pen drawing--Technique',
+    'pen and ink drawing'		=> 'Pen drawing',
+    'pencil drawing'			=> 'Pencil drawing',
+    'pencil drawing--technique'		=> 'Pencil drawing--Technique',
+    'peng, rachel (fictitious character)' => 'Peng, Rachel (Fictitious character)',
+    'peng, rachel'			=> 'Peng, Rachel (Fictitious character)',
+    'perceptual isolation'		=> 'Sensory deprivation',
+    'performance art'			=> 'Performance art',
+    'performance motivation'		=> 'Achievement motivation',
+    'performance pieces'		=> 'Performance art',
+    'periodicals'			=> 'Periodicals',
+    'peripheral computer devices'	=> 'Computer peripherals',
+    'peripheral computer equipment'	=> 'Computer peripherals',
+    'peripheral devices (computers)'	=> 'Computer peripherals',
+    'peripheral equipment (computers)'	=> 'Computer peripherals',
+    'peripherals, computer'		=> 'Computer peripherals',
+    'perl (computer program language)'	=> 'Perl (Computer program language)',
+    'perl'				=> 'Perl (Computer program language)',
+    'perl/tk (computer program language)' => 'Perl/Tk (Computer program language)',
+    'perl/tk'				=> 'Perl/Tk (Computer program language)',
+    'persian gulf war, 1991'		=> 'Persian Gulf War, 1991',
+    'persistent cookies (computer science)'	=> 'Cookies (Computer science)',
+    'persistent cookies'		=> 'Cookies (Computer science)',
+    'personal body care'		=> 'Hygiene',
+    'personal budgets'			=> 'Budgets, Personal',
+    'personal cleanliness'		=> 'Hygiene',
+    'personal computers'		=> 'Microcomputers',
+    'personal finance software'		=> 'Finance, Personal--Software',
+    'personal finance'			=> 'Finance, Personal',
+    'personal financial management'	=> 'Finance, Personal',
+    'personal financial planning'	=> 'Finance, Personal',
+    'personal health services'		=> 'Medical care',
+    'personal hygiene'			=> 'Hygiene',
+    'personal identity'			=> 'Personality',
+    'personal income tax'		=> 'Income tax',
+    'personal time management'		=> 'Time management',
+    'personality'			=> 'Personality',
+    'personality traits'		=> 'Personality',
+    'personification of death'		=> 'Death (Personification)',
+    'personnel administration'		=> 'Personnel management',
+    'personnel management'		=> 'Personnel management',
+    'personology'			=> 'Personality',
+    'pharmaceuticals'			=> 'Drugs',
+    'philosophers'			=> 'Philosophers',
+    'philosophers--biography'		=> 'Philosophers--Biography',
+    'philosophy, moral'			=> 'Ethics',
+    'php (computer program language)'	=> 'PHP (Computer program language)',
+    'php'				=> 'PHP (Computer program language)',
+    'physical endurance'		=> 'Physical fitness',
+    'physical fitness'			=> 'Physical fitness',
+    'physical stamina'			=> 'Physical fitness',
+    'physical-appearance-based bias'	=> 'Physical-appearance-based bias',
+    'physique'				=> 'Bodybuilding',
+    'pickling'				=> 'Canning and preserving',
+    'pictorial data processing'		=> 'Image processing',
+    'picture processing'		=> 'Image processing',
+    'pies'				=> 'Pies',
+    'pies, pizza'			=> 'Pizza',
+    'pizza pies'			=> 'Pizza',
+    'pizza'				=> 'Pizza',
+    'pizzas'				=> 'Pizza',
+    'pl/c (computer program language)'	=> 'PL/C (Computer program language)',
+    'pl/c'				=> 'PL/C (Computer program language)',
+    'pl/sql (computer program language)' => 'PL/SQL (Computer program language)',
+    'pl/sql'				=> 'PL/SQL (Computer program language)',
+    'places, imaginary'			=> 'Imaginary places',
+    'planning'				=> 'Planning',
+    'planning, strategic'		=> 'Strategic planning',
+    'plants in art'			=> 'Plants in art',
+    'plants, psychoactive'		=> 'Psychotropic plants',
+    'plants, psychotropic'		=> 'Psychotropic plants',
+    'pocket calculators'		=> 'Calculators',
+    'poems'				=> 'Poetry',
+    'poetry'				=> 'Poetry',
+    'police'			        => 'Police',
+    'police--england'			=> 'Police--England',
+    'police--england--london'		=> 'Police--England--London',
+    'police--england--london--fiction'	=> 'Police--England--London--Fiction',
+    'political collectibles'		=> 'Political collectibles',
+    'political ethics'			=> 'Political ethics',
+    'political paraphernalia'		=> 'Political collectibles',
+    'political science--moral and ethical aspects' => 'Political ethics',
+    'political terrorism'		=> 'Terrorism',
+    'politics and culture'		=> 'Politics and culture',
+    'politics and culture--united states' => 'Politics and culture--United States',
+    'politics in art'			=> 'Politics in art',
+    'politics, practical'		=> 'Politics, Practical',
+    'politics, practical--moral and ethical aspects' => 'Political ethics',
+    'poll watching'			=> 'Election monitoring',
+    'pop culture'			=> 'Popular culture',
+    'popular arts'			=> 'Popular culture',
+    'popular culture'			=> 'Popular culture',
+    'portraits'				=> 'Portraits',
+    'possible art'			=> 'Conceptual art',
+    'post-object art'			=> 'Conceptual art',
+    'potable liquids'			=> 'Beverages',
+    'potable water'			=> 'Drinking water',
+    'potables'				=> 'Beverages',
+    'pottery'				=> 'Pottery',
+    'pottery, primitive'		=> 'Pottery',
+    'poverty'				=> 'Poverty',
+    'pr'				=> 'Public relations',
+    'practical extraction and report language'	=> 'Perl (Computer program language)',
+    'practical politics'		=> 'Politics, Practical',
+    'prayer, mental'			=> 'Meditation',
+    'precog'				=> 'Precognition',
+    'precognition'			=> 'Precognition',
+    'prepaid health plans'		=> 'Health insurance',
+    'prepaid medical care'		=> 'Health insurance',
+    'prescription drugs'		=> 'Drugs',
+    'presentation graphics software'	=> 'Presentation graphics software',
+    'presentation software'		=> 'Presentation graphics software',
+    'preservation of art objects'       => 'Art objects--Conservation and restoration',
+    'price theory'			=> 'Microeconomics',
+    'princesses'			=> 'Princesses',
+    'print making'			=> 'Prints--Technique',
+    'printmaking'			=> 'Prints--Technique',
+    'prints'				=> 'Prints',
+    'prints--technique'			=> 'Prints--Technique',
+    'privacy'				=> 'Privacy',
+    'private enterprise'		=> 'Free enterprise',
+    'procedural language/sql'		=> 'PL/SQL (Computer program language)',
+    'processing, image'			=> 'Image processing',
+    'processing, word'			=> 'Word processing',
+    'processing, words'			=> 'Word processing',
+    'produce exchanges'			=> 'Commodity exchanges',
+    'product design'			=> 'Product design',
+    'production management'		=> 'Production management',
+    'products, commercial'		=> 'Commercial products',
+    'professions'			=> 'Occupations',
+    'profit-sharing trusts'		=> 'Mutual funds',
+    'programming (electronic computers)' => 'Computer programming',
+    'programming (mathematics)'		=> 'Programming (Mathematics)',
+    'programming languages (computers)' => 'Programming languages (Electronic computers)',
+    'programming languages (electronic computers)' => 'Programming languages (Electronic computers)',
+    'programming languages'		=> 'Programming languages (Electronic computers)',
+    'programming search engines'	=> 'Search engines--Programming',
+    'programming'			=> 'Computer programming',
+    'project management software'	=> 'Project management--Software',
+    'project management'		=> 'Project management',
+    'project management--software'	=> 'Project management--Software',
+    'projection, astral'		=> 'Astral projection',
+    'prolog (computer program language)' => 'Prolog (Computer program language)',
+    'prolog'				=> 'Prolog (Computer program language)',
+    'prolog++ (computer program language)' => 'Prolog++ (Computer program language)',
+    'prolog++'				=> 'Prolog++ (Computer program language)',
+    'property insurance'		=> 'Property insurance',
+    'prophecy'				=> 'Prophecy',
+    'prose poems'			=> 'Prose poems',
+    'prose poetry'			=> 'Prose poems',
+    'prostitution'			=> 'Prostitution',
+    'protocols, computer network'	=> 'Computer network protocols',
+    'provencal cooking'			=> 'Cooking, French--Provençal style',
+    'provençal cooking'			=> 'Cooking, French--Provençal style',
+    'provincial parks'			=> 'Parks',
+    'psychedelic drugs'			=> 'Hallucinogenic drugs',
+    'psychic healing'			=> 'Mental healing',
+    'psychic phenomena'			=> 'Parapsychology',
+    'psychic research'			=> 'Parapsychology',
+    'psychical research'		=> 'Parapsychology',
+    'psycho-kinesis'			=> 'Psychokinesis',
+    'psychoactive plants'		=> 'Psychotropic plants',
+    'psychokinesis'			=> 'Psychokinesis',
+    'psychology'			=> 'Psychology',
+    'psychology, cognitive'		=> 'Cognitive psychology',
+    'psychotomimetic drugs'		=> 'Hallucinogenic drugs',
+    'psychotropic plants'		=> 'Psychotropic plants',
+    'public finance'			=> 'Finance, Public',
+    'public health services'		=> 'Public health',
+    'public health'			=> 'Public health',
+    'public health--planning'		=> 'Health planning',
+    'public hygiene'			=> 'Public health',
+    'public markets'			=> 'Markets',
+    'public relations'			=> 'Public relations',
+    'public transportation'		=> 'Transportation',
+    'punctuation'			=> 'Punctuation',
+    'purchasing and buying'		=> 'Purchasing',
+    'purchasing'			=> 'Purchasing',
+    'purchasing--law and legislation'	=> 'Sales',
+    'purchasing--law'			=> 'Sales',
+    'put and call transactions'		=> 'Options (Finance)',
+    'put options'			=> 'Options (Finance)',
+    'puts (finance)'			=> 'Options (Finance)',
+    'pyramid marketing'			=> 'Multilevel marketing',
+    'pyramid sales clubs'		=> 'Multilevel marketing',
+    'python (computer program language)' => 'Python (Computer program language)',
+    'python'				=> 'Python (Computer program language)',
+    'pythonidae'			=> 'Pythons',
+    'pythoninae'			=> 'Pythons',
+    'pythons'				=> 'Pythons',
+    'q-boats'				=> 'Q-ships',
+    'q-ships'				=> 'Q-ships',
+    'quality assurance'			=> 'Quality assurance',
+    'quality control'			=> 'Quality control',
+    'quality management, total'		=> 'Total quality management',
+    'quality of environment'		=> 'Environmental quality',
+    'quantitative business analysis'	=> 'Management science',
+    'quantity cookery'			=> 'Quantity cooking',
+    'quantity cooking'			=> 'Quantity cooking',
+    'quick and easy cookery'		=> 'Quick and easy cooking',
+    'quick and easy cooking'		=> 'Quick and easy cooking',
+    'quick-meal cookery'		=> 'Quick and easy cooking',
+    'quick-meal cooking'		=> 'Quick and easy cooking',
+    'r & d projects'			=> 'Research and development projects',
+    'r & d'				=> 'Research and development projects',
+    'r and d projects'			=> 'Research and development projects',
+    'r&d projects'			=> 'Research and development projects',
+    'r&d'				=> 'Research and development projects',
+    'rabbits'				=> 'Rabbits',
+    'rachel peng'			=> 'Peng, Rachel (Fictitious character)',
+    'radio vision'			=> 'Television',
+    'rationalization of industry'	=> 'Industrial management',
+    'raw food'				=> 'Raw foods',
+    'raw foods'				=> 'Raw foods',
+    'real estate investment'		=> 'Real estate investment',
+    'real estate'			=> 'Real property',
+    'real property investment'		=> 'Real estate investment',
+    'real property'			=> 'Real property',
+    'reducing diets'			=> 'Reducing diets',
+    'reduction of weight'		=> 'Weight loss',
+    'realty'				=> 'Real property',
+    'recreational shooting'		=> 'Shooting',
+    'regional economics'		=> 'Regional economics',
+    'regional parks'			=> 'Parks',
+    'reicki (healing system)'		=> 'Reiki (Healing system)',
+    'reicki'				=> 'Reiki (Healing system)',
+    'reiki (healing system)'		=> 'Reiki (Healing system)',
+    'reiki'				=> 'Reiki (Healing system)',
+    'reincarnation'			=> 'Reincarnation',
+    'religion and politics'		=> 'Religion and politics',
+    'religion and politics--united states' => 'Religion and politics--United States',
+    'religion and social problems'	=> 'Religion and social problems',
+    'religious art'			=> 'Religious art',
+    'religious corporations'		=> 'Corporations, Religious',
+    'report program generator'		=> 'RPG (Computer program language)',
+    'research & development projects'	=> 'Research and development projects',
+    'research and development projects'	=> 'Research and development projects',
+    'resistance to government'		=> 'Government, Resistance to',
+    'resources, natural'		=> 'Natural resources',
+    'restoration of art objects'	=> 'Art objects--Conservation and restoration',
+    'resumes'				=> 'Résumés (Employment)',
+    'resumes (employment)'		=> 'Résumés (Employment)',
+    'résumés'				=> 'Résumés (Employment)',
+    'résumés (employment)'		=> 'Résumés (Employment)',
+    'retail industry'			=> 'Retail trade',
+    'retail marketing'			=> 'Marketing',
+    'retail trade'			=> 'Retail trade',
+    'retail trade--marketing'		=> 'Marketing',
+    'retail franchises'			=> 'Franchises (Retail trade)',
+    'retailing'				=> 'Retail trade',
+    'retirement planning'		=> 'Retirement--Planning',
+    "rights of women"			=> "Women's rights",
+    'retirement'			=> 'Retirement',
+    'retirement--planning'		=> 'Retirement--Planning',
+    'ridgway reservoir (colo.)'  	=> 'Ridgway Reservoir (Colo.)',
+    'ridgway'				=> 'Ridgway Reservoir (Colo.)',
+    'right and left (political science)' => 'Right and left (Political science)',
+    'rights, civil'			=> 'Civil rights',
+    'risk (insurance)'			=> 'Risk (Insurance)',
+    'risk'				=> 'Risk',
+    'rivers'				=> 'Rivers',
+    'rivers--england'			=> 'Rivers--England',
+    'romance'				=> 'Love stories',
+    'routines, utility'			=> 'Utilities (Computer programs)',
+    'rpg (computer program language)'	=> 'RPG (Computer program language)',
+    'rubies'				=> 'Rubies',
+    'ruby (computer program language)'	=> 'Ruby (Computer program language)',
+    'rulers'				=> 'Heads of state',
+    'russian art'			=> 'Art, Russian',
+    'safety measures'			=> 'Safety measures',
+    'salads'				=> 'Salads',
+    'sales management'			=> 'Sales management',
+    'sales'				=> 'Sales',
+    'sales--law and legislation'	=> 'Sales',
+    'sales--law'			=> 'Sales',
+    'salesmanship'			=> 'Selling',
+    'salesmen and salesmanship'		=> 'Selling',
+    'salt-free diet'			=> 'Salt-free diet',
+    'sam vimes (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
+    'samuel vimes (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
+    'sanitary affairs'			=> 'Public health',
+    'satire'				=> 'Satire',
+    'sauces'				=> 'Sauces',
+    'schleswig-holstein cooking'	=> 'Cooking, German--Schleswig-Holstein style',
+    'schooling'				=> 'Education',
+    'sci fi'	 			=> 'Science fiction',
+    'sci-fi'	 			=> 'Science fiction',
+    'sci/fi'	 			=> 'Science fiction',
+    'science fiction & fantasy'		=> 'Speculative fiction',
+    'science fiction and fantasy'	=> 'Speculative fiction',
+    'science fiction' 			=> 'Science fiction',
+    'science fiction--authorship'	=> 'Science fiction--Authorship',
+    'science fiction--general'		=> 'Science fiction',
+    'science fiction--high tech'	=> 'High technology',
+    'science fiction--technique'	=> 'Science fiction--Technique',
+    'science fiction/fantasy'		=> 'Speculative fiction',
+    'science of language'		=> 'Linguistics',
+    'science of science'		=> 'Science',
+    'science stories'			=> 'Science fiction',
+    'science'				=> 'Science',
+    'science, moral'			=> 'Ethics',
+    'science--authorship'		=> 'Technical writing',
+    'science--fiction'			=> 'Science fiction',
+    'sciences'				=> 'Science',
+    'scientific writing'		=> 'Technical writing',
+    'scifi'	 			=> 'Science fiction',
+    'scotland'				=> 'Scotland',
+    'scottish cooking'			=> 'Cooking, Scottish',
+    'screen trading (securities)'	=> 'Electronic trading of securities',
+    'screen trading'			=> 'Electronic trading of securities',
+    'sculpting'				=> 'Sculpture--Technique',
+    'sculpture'				=> 'Sculpture',
+    'sculpture--technique'		=> 'Sculpture--Technique',
+    'search agents'			=> 'Search engines',
+    'search engines'			=> 'Search engines',
+    'search engines, web'		=> 'Web search engines',
+    'search engines--programming'	=> 'Search engines--Programming',
+    'second sight'			=> 'Precognition',
+    'second world war'			=> 'World War, 1939-1945',
+    'secret writing'			=> 'Cryptography',
+    'secretarial aids & training'	=> 'Secretaries--Training',
+    'secretarial aids and training'	=> 'Secretaries--Training',
+    'secretaries'			=> 'Secretaries',
+    'secretaries--training'		=> 'Secretaries--Training',
+    'security of computer systems'	=> 'Computer security',
+    'security of computer networks'	=> 'Computer networks--Security measures',
+    'self-improvement'			=> 'Success',
+    'selling homes'			=> 'House selling',
+    'selling houses'			=> 'House selling',
+    'selling'				=> 'Selling',
+    'selling--law and legislation'	=> 'Sales',
+    'selling--law'			=> 'Sales',
+    'sensory deprivation therapy'	=> 'Sensory deprivation--Therapeutic use',
+    'sensory deprivation'		=> 'Sensory deprivation',
+    'sensory deprivation--therapeutic use' => 'Sensory deprivation--Therapeutic use',
+    'sensory isolation'			=> 'Sensory deprivation',
+    'seraphim'				=> 'Angels',
+    'serial picture books'		=> 'Comic books, strips, etc',
+    'service industries'		=> 'Service industries',
+    'services, contracting for'		=> 'Contracting out',
+    'services, financial'		=> 'Financial services industry',
+    'sex'				=> 'Sex',
+    'sex--religious aspects'		=> 'Sex--Religious aspects',
+    'sexual abuse of children'		=> 'Child sexual abuse',
+    'sexual behavior'			=> 'Sex',
+    'sexual child abuse'		=> 'Child sexual abuse',
+    'sexual practices'			=> 'Sex',
+    'sexuality'				=> 'Sex',
+    'shamanism'				=> 'Shamanism',
+    'shape-shifters'			=> 'Shapeshifting',
+    'shape-shifting'			=> 'Shapeshifting',
+    'shapeshifters'			=> 'Shapeshifting',
+    'shapeshifting'			=> 'Shapeshifting',
+    'shapeshifting--fiction'		=> 'Shapeshifting',
+    'shares of stock'			=> 'Stocks',
+    'shooting sport'			=> 'Shooting',
+    'shooting sports'			=> 'Shooting',
+    'shooting'				=> 'Shooting',
+    'short stories'			=> 'Short stories',
+    'sickness insurance'		=> 'Health insurance',
+    'simulation, computer'		=> 'Computer simulation',
+    'sisters'				=> 'Sisters',
+    'sisters--fiction'			=> 'Sisters--Fiction',
+    'sites, web'			=> 'Web sites',
+    'sixth sense'			=> 'Extrasensory perception',
+    'sizeism'				=> 'Physical-appearance-based bias',
+    'sizism'				=> 'Physical-appearance-based bias',
+    'skills training'			=> 'Training',
+    'slate computers'			=> 'Tablet computers',
+    'slave holders'			=> 'Slaveholders',
+    'slave keeping'			=> 'Slavery',
+    'slave master'			=> 'Slaveholders',
+    'slave masters'			=> 'Slaveholders',
+    'slave owner'			=> 'Slaveholders',
+    'slave owners'			=> 'Slaveholders',
+    'slave ownership'			=> 'Slavery',
+    'slave system'			=> 'Slavery',
+    'slaveholders'			=> 'Slaveholders',
+    'slaveholding'			=> 'Slavery',
+    'slavemasters'			=> 'Slaveholders',
+    'slaveowners'			=> 'Slaveholders',
+    'slavery'				=> 'Slavery',
+    'sleep deprivation'			=> 'Sleep deprivation',
+    'sleep'				=> 'Sleep',
+    'sleeping'				=> 'Sleep',
+    'slimming'				=> 'Weight loss',
+    'slumber'				=> 'Sleep',
+    'small and medium-sized business'	=> 'Small business',
+    'small and medium-sized businesses'	=> 'Small business',
+    'small and medium-sized enterprise'	=> 'Small business',
+    'small and medium-sized enterprises' => 'Small business',
+    'small arms'			=> 'Firearms',
+    'small business tax'		=> 'Small business--Taxation',
+    'small business taxation'		=> 'Small business--Taxation',
+    'small business taxes'		=> 'Small business--Taxation',
+    'small business'			=> 'Small business',
+    'small business--Taxation'		=> 'Small business--Taxation',
+    'small businesses'			=> 'Small business',
+    'smalltalk (computer program language)' => 'Smalltalk (Computer program language)',
+    'smart growth'			=> 'Sustainable development',
+    'sneffels' 				=> 'Mount Sneffels Wilderness (Colo.)',
+    'social action'			=> 'Social action',
+    'social action--research'		=> 'Action research',
+    'social action--religious aspects'	=> 'Religion and social problems',
+    'social classes'			=> 'Social classes',
+    'social classes--fiction'		=> 'Social classes--Fiction',
+    'social exclusion'			=> 'Social isolation',
+    'social hygiene'			=> 'Public health',
+    'social infrastructure'		=> 'Infrastructure (Economics)',
+    'social isolation'			=> 'Social isolation',
+    'social medicine'			=> 'Social medicine',
+    'social networking web sites'	=> 'Online social networks',
+    'social networking'			=> 'Social networks',
+    'social networks'			=> 'Social networks',
+    'social overhead capital'		=> 'Infrastructure (Economics)',
+    'social problems and religion'	=> 'Religion and social problems',
+    'social support systems'		=> 'Social networks',
+    'socioeconomic status'		=> 'Economic conditions',
+    'sodium-restricted diet'		=> 'Salt-free diet',
+    'software development'		=> 'Computer software--Development',
+    'software engineering'		=> 'Software engineering',
+    'software engineering--project management' => 'Software engineering--Project management',
+    'software'				=> 'Computer software',
+    'software, computer'		=> 'Computer software',
+    'software, malevolent'		=> 'Malware (Computer software)',
+    'software, malicious'		=> 'Malware (Computer software)',
+    'soho (london, england)'		=> 'Soho (London, England)',
+    'soothsaying'			=> 'Soothsaying',
+    'sorcerers'				=> 'Wizards',
+    'sorrow'				=> 'Grief',
+    'soup'				=> 'Soups',
+    'soups'				=> 'Soups',
+    'south german cooking'		=> 'Cooking, German--Southern style',
+    'southern cooking (united states)'	=> 'Cooking, American--Southern style',
+    'southern cooking (germany)'	=> 'Cooking, German--Southern style',
+    'southwestern cooking (united states)'	=> 'Cooking, American--Southwestern style',
+    'space colonies'			=> 'Space colonies',
+    'space opera'			=> 'Speculative fiction',
+    'space rockets'			=> 'Space vehicles',
+    'space ships'			=> 'Space vehicles',
+    'space vehicles'			=> 'Space vehicles',
+    'spacecraft'			=> 'Space vehicles',
+    'spaceships'			=> 'Space vehicles',
+    'spam (electronic mail)'		=> 'Spam (Electronic mail)',
+    'spam'				=> 'Spam (Electronic mail)',
+    'specfic'				=> 'Speculative fiction',
+    'specie'				=> 'Coins',
+    'speculative fantasy'		=> 'Speculative fiction',
+    'speculative fiction'		=> 'Speculative fiction',
+    'speech processing systems'		=> 'Speech processing systems',
+    'speech processing'			=> 'Speech processing systems',
+    'spices--use in cooking'		=> 'Cooking (Spices)',
+    'spies'     			=> 'Spy stories',
+    'spirit channeling'			=> 'Channeling (Spiritualism)',
+    'spirit guides'			=> 'Guides (Spiritualism)',
+    'spiritism'				=> 'Spiritualism',
+    'spirits, alcoholic'		=> 'Liquors',
+    'spiritual healing'			=> 'Spiritual healing',
+    'spiritual therapies'		=> 'Spiritual healing',
+    'spiritual-mindedness'		=> 'Spirituality',
+    'spiritualism'			=> 'Spiritualism',
+    'spirituality'			=> 'Spirituality',
+    'spirituous liquors'		=> 'Liquors',
+    'sport shooting'			=> 'Shooting',
+    'sports stories'			=> 'Sports stories',
+    'sports'				=> 'Sports',
+    'sports--fiction'			=> 'Sports stories',
+    'sports--juvenile fiction'		=> 'Sports stories',
+    'spread sheets'			=> 'Electronic spreadsheets',
+    'spread sheets, electronic'		=> 'Electronic spreadsheets',
+    'spreadsheet software'		=> 'Electronic spreadsheets--Software',
+    'spreadsheeting, electronic'	=> 'Electronic spreadsheets',
+    'spreadsheets'			=> 'Electronic spreadsheets',
+    'spreadsheets, electronic'		=> 'Electronic spreadsheets',
+    'spy stories'     			=> 'Spy stories',
+    'sql (computer program language)'	=> 'SQL (Computer program language)',
+    'sql'				=> 'SQL (Computer program language)',
+    'standard of value'			=> 'Money',
+    'star trek fiction'			=> 'Star Trek fiction',
+    'star trek'				=> 'Star Trek fiction',
+    'star wars fiction'			=> 'Star Wars fiction',
+    'star wars'				=> 'Star Wars fiction',
+    'start-up business enterprises'	=> 'New business enterprises',
+    'start-up businesses enterprises'	=> 'New business enterprises',
+    'start-up companies'		=> 'New business enterprises',
+    'start-up enterprises'		=> 'New business enterprises',
+    'start-ups (business enterprises)'	=> 'New business enterprises',
+    'start-ups'				=> 'New business enterprises',
+    'starting a business'		=> 'New business enterprises',
+    'startups (business enterprises)'	=> 'New business enterprises',
+    'startups'				=> 'New business enterprises',
+    'state parks'			=> 'Parks',
+    'statistical matching'		=> 'Statistical matching',
+    'statues'				=> 'Sculpture',
+    'statuettes'			=> 'Figurines',
+    'statutes'				=> 'Statutes',
+    'steampunk fiction'			=> 'Steampunk fiction',
+    'steampunk'				=> 'Steampunk fiction',
+    'steganography'			=> 'Cryptography',
+    'stereograms'			=> 'Stereograms',
+    'stew'				=> 'Stews',
+    'stews'				=> 'Stews',
+    'stimulus deprivation'		=> 'Sensory deprivation',
+    'stock issues'			=> 'Stocks',
+    'stock offerings'			=> 'Stocks',
+    'stock trading'			=> 'Stocks',
+    'stocks'				=> 'Stocks',
+    'stonework, decorative'		=> 'Sculpture',
+    'strategic management'		=> 'Strategic planning',
+    'strategic planning'		=> 'Strategic planning',
+    'street art'			=> 'Street art',
+    'structural adjustment (economic policy)' => 'Structural adjustment (Economic policy)',
+    'structural adjustment'		=> 'Structural adjustment (Economic policy)',
+    'structured query language'		=> 'SQL (Computer program language)',
+    'study guides'			=> 'Study guides',
+    'submarine boats'			=> 'Submarines (Ships)',
+    'submarines (ships)'		=> 'Submarines (Ships)',
+    'submarines'			=> 'Submarines (Ships)',
+    'success'				=> 'Success',
+    'super heroes'			=> 'Superheroes',
+    'superannuation'			=> 'Retirement',
+    'superheroes'			=> 'Superheroes',
+    'supernatural'			=> 'Supernatural',
+    'survival'				=> 'Survival',
+    'survival skills'			=> 'Survival',
+    'suspense fiction'			=> 'Suspense fiction',
+    'suspense tales'			=> 'Suspense fiction',
+    'suspense'				=> 'Suspense fiction',
+    'sustainable development'		=> 'Sustainable development',
+    'sustainable economic development'	=> 'Sustainable development',
+    'sweets'				=> 'Confectionery',
+    'system administration'		=> 'Computers--Administration',
+    'system analysis'			=> 'System analysis',
+    'systems administration'		=> 'Computers--Administration',
+    'systems analysis'			=> 'System analysis',
+    'systems architecture'		=> 'Computer architecture',
+    'systems, expert (computer science)' => 'Expert systems (Computer science)',
+    'systems, expert'			=> 'Expert systems (Computer science)',
+    'tablet computers'			=> 'Tablet computers',
+    'tablets (computers)'		=> 'Tablet computers',
+    'takeovers, corporate'		=> 'Consolidation and merger of corporations',
+    'tap water'				=> 'Drinking water',
+    'tarot'				=> 'Tarot',
+    'tattooing'				=> 'Tattooing',
+    'tattoos (body markings)'		=> 'Tattooing',
+    'tattoos'				=> 'Tattooing',
+    'tax policy'			=> 'Taxation',
+    'tax reform'			=> 'Taxation',
+    'tax-exempt organizations'		=> 'Nonprofit organizations',
+    'taxable income'			=> 'Income tax',
+    'taxation of franchises'		=> 'Corporations--Taxation',
+    'taxation of income'		=> 'Income tax',
+    'taxation'				=> 'Taxation',
+    'taxation, incidence of'		=> 'Taxation',
+    'taxes'				=> 'Taxation',
+    'tea'				=> 'Tea',
+    'tea--use in cooking'		=> 'Cooking (Tea)',
+    'teachers'				=> 'Teachers',
+    'technical writing'			=> 'Technical writing',
+    'technology'			=> 'Technology',
+    'technology--authorship'		=> 'Technical writing',
+    'technomancers'			=> 'Technomancers',
+    'technomancy'			=> 'Technomancy',
+    'telekinesis'			=> 'Psychokinesis',
+    'telemarketing'			=> 'Telemarketing',
+    'telepathy'				=> 'Telepathy',
+    'telephone marketing'		=> 'Telemarketing',
+    'telephone service'			=> 'Telephone',
+    'telephone'				=> 'Telephone',
+    'telephones'			=> 'Telephone',
+    'teleportation'			=> 'Teleportation',
+    'teleprocessing networks'		=> 'Computer networks',
+    'television'			=> 'Television',
+    'territorial parks'			=> 'Parks',
+    'terror attacks'			=> 'Terrorism',
+    'terrorism'				=> 'Terrorism',
+    'terrorism, acts of'		=> 'Terrorism',
+    'terrorist acts'			=> 'Terrorism',
+    'terrorist attacks'			=> 'Terrorism',
+    'textile industry and fabrics'	=> 'Textile industry',
+    'textile industry'			=> 'Textile industry',
+    'textiles industry'			=> 'Textile industry',
+    'thames'                 		=> 'Thames River (England)',
+    'thames river'           		=> 'Thames River (England)',
+    'thames river (england)' 		=> 'Thames River (England)',
+    'theology'				=> 'Theology',
+    'theology, mystical'		=> 'Mysticism',
+    'thieves'				=> 'Thieves',
+    'thinking, artificial'		=> 'Artificial intelligence',
+    'thought-transference'		=> 'Telepathy',
+    'thralldom'				=> 'Slavery',
+    'thriller'				=> 'Suspense fiction',
+    'thrillers (fiction)'		=> 'Suspense fiction',
+    'thrillers'				=> 'Suspense fiction',
+    'tiffany aching (fictitious character)' => 'Aching, Tiffany (Fictitious character)',
+    'tiffany aching'			=> 'Aching, Tiffany (Fictitious character)',
+    'time allocation'			=> 'Time management',
+    'time budgets'			=> 'Time management',
+    'time management'			=> 'Time management',
+    'time use'				=> 'Time management',
+    'time'				=> 'Time',
+    'time--management'			=> 'Time management',
+    'time--organization'		=> 'Time management',
+    'time--use of'			=> 'Time management',
+    'time-saving cooking'		=> 'Quick and easy cooking',
+    'timepieces'			=> 'Clocks and watches',
+    'total quality management'		=> 'Total quality management',
+    'tour operators'			=> 'Tourism',
+    'tourism industry'			=> 'Tourism',
+    'tourism operators'			=> 'Tourism',
+    'tourism'				=> 'Tourism',
+    'tourist industry'			=> 'Tourism',
+    'tourist trade'			=> 'Tourism',
+    'tourist traffic'			=> 'Tourism',
+    'towns'				=> 'Cities and towns',
+    'tqm (total quality management)'	=> 'Total quality management',
+    'tqm'				=> 'Total quality management',
+    'trade policy'			=> 'Commercial policy',
+    'trade'				=> 'Commerce',
+    'trade, international'		=> 'International trade',
+    'trades'				=> 'Occupations',
+    'trading of securities, electronic'	=> 'Electronic trading of securities',
+    'trading, futures'			=> 'Futures',
+    'trading, stock'			=> 'Stocks',
+    'training'				=> 'Training',
+    'transmission of data'		=> 'Data transmission systems',
+    'transnational corporations'	=> 'International business enterprises',
+    'transnational enterprises'		=> 'International business enterprises',
+    'transport industry'		=> 'Transportation',
+    'transport'				=> 'Transportation',
+    'transportation companies'		=> 'Transportation',
+    'transportation industry'		=> 'Transportation',
+    'transportation'			=> 'Transportation',
+    'travel industry'			=> 'Tourism',
+    'travel, astral'			=> 'Astral projection',
+    'tv'				=> 'Television',
+    'u.s. government'			=> 'United States--Politics and government',
+    'u.s. political culture'		=> 'Politics and culture--United States',
+    'u.s. politics'			=> 'United States--Politics and government',
+    'u.s.a.'				=> 'United States',
+    'ufo phenomena'			=> 'Unidentified flying objects',
+    'ufo'				=> 'Unidentified flying objects',
+    'ufology'				=> 'Unidentified flying objects',
+    'ufos'				=> 'Unidentified flying objects',
+    'uits'				=> 'Mutual funds',
+    'uml (computer science)'		=> 'UML (Computer science)',
+    'uml'				=> 'UML (Computer science)',
+    'uncooked food'			=> 'Raw foods',
+    'underwater basket weaving'		=> 'Underwater construction--Baskets',
+    'underwater basket-weaving'		=> 'Underwater construction--Baskets',
+    'underwater construction'		=> 'Underwater construction',
+    'underwater construction--baskets'	=> 'Underwater construction--Baskets',
+    'underwriting'			=> 'Insurance',
+    'unemployment'			=> 'Unemployment',
+    'unfired food'			=> 'Raw foods',
+    'unidentified flying objects'	=> 'Unidentified flying objects',
+    'unified modeling language'		=> 'Unified Modeling Language',
+    'unit investment trusts'		=> 'Mutual funds',
+    'unit trusts'			=> 'Mutual funds',
+    'united states political culture'	=> 'Politics and culture--United States',
+    'united states'			=> 'United States',
+    'united states, northwestern'	=> 'Northwestern States',
+    'united states--collectibles'	=> 'Americana',
+    'united states--politics and culture' => 'Politics and culture--United States',
+    'united states--politics and government' => 'United States--Politics and government',
+    'urban economics'			=> 'Urban economics',
+    'us government'			=> 'United States--Politics and government',
+    'us political culture'		=> 'Politics and culture--United States',
+    'us politics'			=> 'United States--Politics and government',
+    'us'				=> 'United States',
+    'usa'				=> 'United States',
+    'use of time'			=> 'Time management',
+    'user created content'		=> 'User-generated content',
+    'user generated content'		=> 'User-generated content',
+    'user interfaces (computer systems)' => 'User interfaces (Computer systems)',
+    'user interfaces'			 => 'User interfaces (Computer systems)',
+    'user-created content'		=> 'User-generated content',
+    'user-generated content'		=> 'User-generated content',
+    'usury'				=> 'Interest',
+    'utensils'				=> 'Implements, utensils, etc',
+    'utilities (computer programs)'	=> 'Utilities (Computer programs)',
+    'utility programs'			=> 'Utilities (Computer programs)',
+    'utility routines'			=> 'Utilities (Computer programs)',
+    'valdemar'			        => 'Valdemar (Imaginary place)',
+    'valdemar (imaginary place)'        => 'Valdemar (Imaginary place)',
+    'vampires'				=> 'Vampires',
+    'vbscript (computer program language)' => 'VBScript (Computer program language)',
+    'vbscript'				=> 'VBScript (Computer program language)',
+    'vessels (utensils)'		=> 'Implements, utensils, etc',
+    'video art'				=> 'Video art',
+    'video games'			=> 'Computer games',
+    'vietnam'				=> 'Vietnam',
+    'vietnam war'			=> 'Vietnam War, 1961-1975',
+    'vietnam war, 1961-1975'		=> 'Vietnam War, 1961-1975',
+    'vimes, samuel (fictitious character)'	=> 'Vimes, Samuel (Fictitious character)',
+    'vintage automobiles'		=> 'Antique and classic cars',
+    'vintage cars'			=> 'Antique and classic cars',
+    'virtual computer systems'		=> 'Virtual computer systems',
+    'virtual corporations'		=> 'Virtual corporations',
+    'virtual environments'		=> 'Virtual reality',
+    'virtual machine systems'		=> 'Virtual computer systems',
+    'virtual machines'			=> 'Virtual computer systems',
+    'virtual private networks'		=> 'Extranets (Computer networks)',
+    'virtual reality'			=> 'Virtual reality',
+    'virtual worlds'			=> 'Virtual reality',
+    'vision, computer'			=> 'Computer vision',
+    'vision, machine'			=> 'Computer vision',
+    'visitor industry'			=> 'Tourism',
+    'visual data processing'		=> 'Optical data processing',
+    'voyagers'				=> 'Explorers',
+    'vpns'				=> 'Extranets (Computer networks)',
+    'vr'				=> 'Virtual reality',
+    'w3 (world wide web)'		=> 'World Wide Web',
+    'w3'				=> 'World Wide Web',
+    'wall art'				=> 'Street art',
+    'war stories'			=> 'War stories',
+    'war'				=> 'War stories',
+    'warehousing data'			=> 'Data warehousing',
+    'warehousing information'		=> 'Data warehousing',
+    'warehousing, data'			=> 'Data warehousing',
+    'warehousing, information'		=> 'Data warehousing',
+    'warlocks'				=> 'Warlocks',
+    'warships'				=> 'Warships',
+    'watches'				=> 'Clocks and watches',
+    'water-color painting'		=> 'Watercolor painting',
+    'water-color paintings'		=> 'Watercolor painting',
+    'water-colors'			=> 'Watercolor painting',
+    'watercolor painting'		=> 'Watercolor painting',
+    'watercolor painting--technique'	=> 'Watercolor painting--Technique',
+    'watercolor paintings'		=> 'Watercolor painting',
+    'watercolors'			=> 'Watercolor painting',
+    'weather'				=> 'Weather',
+    'weatherwax, esme (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'weatherwax, esmerelda (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'weatherwax, granny (fictitious character)'	=> 'Weatherwax, Granny (Fictitious character)',
+    'web (world wide web)'		=> 'World Wide Web',
+    'web broadcasting'			=> 'Webcasting',
+    'web browsers'			=> 'Browsers (Computer programs)',
+    'web browsing software'		=> 'Browsers (Computer programs)',
+    'web casting'			=> 'Webcasting',
+    'web drugstores'			=> 'Internet pharmacies',
+    'web logs'				=> 'Blogs',
+    'web pages'				=> 'Web sites',
+    'web pharmacies'			=> 'Internet pharmacies',
+    'web programming'			=> 'Web site development',
+    'web retailing'			=> 'Electronic commerce',
+    'web search engines'		=> 'Web search engines',
+    'web searching'			=> 'Web search engines',
+    'web services'			=> 'Web services',
+    'web site development'		=> 'Web site development',
+    'web site directories'		=> 'Web sites--Directories',
+    'web site programming'		=> 'Web site development',
+    'web sites'				=> 'Web sites',
+    'web sites--computer programming'	=> 'Web site development',
+    'web sites--design'			=> 'Web sites--Design',
+    'web sites--development'		=> 'Web site development',
+    'web sites--directories'		=> 'Web sites--Directories',
+    'web sites--programming'		=> 'Web site development',
+    'webcasting'			=> 'Webcasting',
+    'webliographies'			=> 'Web sites--Directories',
+    'weblogs'				=> 'Blogs',
+    'webpage design'			=> 'Web sites--Design',
+    'webpages'				=> 'Web sites',
+    'website design'			=> 'Web sites--Design',
+    'website development'		=> 'Web site development',
+    'website programming'		=> 'Web site development',
+    'websites'				=> 'Web sites',
+    'weight control of obesity'		=> 'Weight loss',
+    'weight control'			=> 'Weight loss',
+    'weight loss'			=> 'Weight loss',
+    'weight reducing'			=> 'Weight loss',
+    'weight reduction'			=> 'Weight loss',
+    'welsh cooking'			=> 'Cooking, Welsh',
+    'were-wolf'				=> 'Werewolves',
+    'were-wolves'			=> 'Werewolves',
+    'werewolf'				=> 'Werewolves',
+    'werewolves'			=> 'Werewolves',
+    'werewolves--fiction'		=> 'Werewolves',
+    'werwolf'				=> 'Werewolves',
+    'werwolves'				=> 'Werewolves',
+    'west indian cooking'		=> 'Cooking, West Indian',
+    'western cooking (united states)'	=> 'Cooking, American--Western style',
+    'westphalian cooking'		=> 'Cooking, German--Westphalian style',
+    'wideband communication systems'	=> 'Broadband communication systems',
+    'wimmin'				=> 'Women',
+    'wireless communication systems'	=> 'Wireless communication systems',
+    'wireless data communication systems' => 'Wireless communication systems',
+    'wireless data communication'	=> 'Wireless communication systems',
+    'wireless data transmission systems' => 'Wireless communication systems',
+    'wireless data transmission'	=> 'Wireless communication systems',
+    'wireless information systems'	=> 'Wireless communication systems',
+    'wireless telecommunication systems' => 'Wireless communication systems',
+    'wireless telecommunication'	=> 'Wireless communication systems',
+    'witchcraft'			=> 'Witchcraft',
+    'wizard of london'			=> 'Wizards--England--London--Fiction',
+    'wizards'				=> 'Wizards',
+    'wizards--england--london--fiction'	=> 'Wizards--England--London--Fiction',
+    'wok cookery'			=> 'Wok cooking',
+    'wok cooking'			=> 'Wok cooking',
+    'woman'				=> 'Women',
+    'women'				=> 'Women',
+    'women detectives'			=> 'Women detectives',
+    'women entrepreneurs'		=> 'Businesswomen',
+    'women heroes'			=> 'Women heroes',
+    'women heroes--fiction'		=> 'Women heroes--Fiction',
+    'women in business'			=> 'Businesswomen',
+    'women--biography'			=> 'Women--Biography',
+    "women--civil rights"		=> "Women's rights",
+    "women--rights of women"		=> "Women's rights",
+    'women--suffrage'			=> 'Women--Suffrage',
+    "women's rights"			=> "Women's rights",
+    'womon'				=> 'Women',
+    'womyn'				=> 'Women',
+    'wood furniture'			=> 'Furniture',
+    'wooden furniture'			=> 'Furniture',
+    'wooing'				=> 'Courtship',
+    'word processing software'		=> 'Word processing--Software',
+    'word processing'			=> 'Word processing',
+    'word processing--software'		=> 'Word processing--Software',
+    'work environment'			=> 'Work environment',
+    'working conditions'		=> 'Work environment',
+    'working conditions, physical'	=> 'Work environment',
+    'working environment'		=> 'Work environment',
+    'workplace climate'			=> 'Work environment',
+    'workplace culture'			=> 'Work environment',
+    'workplace environment'		=> 'Work environment',
+    'workplace'				=> 'Work environment',
+    'worksite environment'		=> 'Work environment',
+    'world economics'			=> 'Competition, International',
+    'world terrorism'			=> 'Terrorism',
+    'world trade'			=> 'International trade',
+    'world war, 1939-1945'		=> 'World War, 1939-1945',
+    'world war 2'			=> 'World War, 1939-1945',
+    'world war ii'			=> 'World War, 1939-1945',
+    'world war two'			=> 'World War, 1939-1945',
+    'world wide web'			=> 'World Wide Web',
+    'world wide web pages'		=> 'Web sites',
+    'world wide web searching'		=> 'Web search engines',
+    'world wide web sites'		=> 'Web sites',
+    'world wide web sites--directories'	=> 'Web sites--Directories',
+    'world wide web--directories'	=> 'Web sites--Directories',
+    'writing'				=> 'Writing',
+    'writing letters'			=> 'Letter writing',
+    'writing of letters'		=> 'Letter writing',
+    'www (world wide web)'		=> 'World Wide Web',
+    'www pages'				=> 'Web sites',
+    'www sites'				=> 'Web sites',
+    'www'				=> 'World Wide Web',
+    'xml (document markup language)'	=> 'XML (Document markup language)',
+    'xml'				=> 'XML (Document markup language)',
+    'xml-based user interface language'	=> 'XUL (Document markup language)',
+    'xul (document markup language)'	=> 'XUL (Document markup language)',
+    'xul'				=> 'XUL (Document markup language)',
+    'young women'			=> 'Young women',
+    'young women--england--fiction'	=> 'Young women--England--Fiction',
+    'young women--fiction'		=> 'Young women--Fiction',
+    'youngsters'			=> 'Children',
+    'zombies'				=> 'Zombies',
+    'zombis'				=> 'Zombies',
+   );
+
+
 our %mobibooktypes = (
     'Default' => undef,
     'eBook' => 'text/x-oeb1-document',
@@ -336,52 +3936,88 @@ our %mobibooktypes = (
     );
 
 our %publishermap = (
-    'ace'                   => 'Ace Books',
-    'acebooks'              => 'Ace Books',
-    'ace books'             => 'Ace Books',
-    'baen'                  => 'Baen Publishing Enterprises',
-    'baen publishing'       => 'Baen Publishing Enterprises',
-    'ballantine'            => 'Ballantine Books',
-    'ballantine books'      => 'Ballantine Books',
-    'barnes and noble'      => 'Barnes and Noble Publishing',
-    'barnesandnoble.com'    => 'Barnes and Noble Publishing',
-    'cpan'                  => 'CPAN',
-    'delrey'                => 'Del Rey Books',
-    'del rey'               => 'Del Rey Books',
-    'del rey books'         => 'Del Rey Books',
-    'e-reads'               => 'E-Reads',
-    'ereads'                => 'E-Reads',
-    'ereads.com'            => 'E-Reads',
-    'www.ereads.com'        => 'E-Reads',
-    'feedbooks'             => 'Feedbooks',
-    'feedbooks (www.feedbooks.com)' => 'Feedbooks',
-    'www.feedbooks.com'     => 'Feedbooks',
-    'fictionwise'           => 'Fictionwise',
-    'fictionwise.com'       => 'Fictionwise',
-    'www.fictionwise.com'   => 'Fictionwise',
-    'harmony'               => 'Harmony Books',
-    'harmony books'         => 'Harmony Books',
-    'harpercollins'         => 'HarperCollins',
-    'harper collins'        => 'HarperCollins',
-    'harper-collins'        => 'HarperCollins',
-    'manybooks'             => 'ManyBooks',
-    'manybooks.net'         => 'ManyBooks',
-    'project gutenberg'     => 'Project Gutenberg',
-    'gutenberg'             => 'Project Gutenberg',
-    'gutenberg.org'         => 'Project Gutenberg',
-    'www.gutenberg.org'     => 'Project Gutenberg',
-    'randomhouse'           => 'Random House',
-    'randomhouse.co.uk'     => 'Random House',
-    'www.randomhouse.co.uk' => 'Random House',
-    'random-house.com'      => 'Random House',
-    'www.random-house.com'  => 'Random House',
-    'rosetta'               => 'Rosetta Books',
-    'rosettabooks'          => 'Rosetta Books',
-    'rosetta books'         => 'Rosetta Books',
-    'siren'                 => 'Siren Publishing',
-    'siren publishing'      => 'Siren Publishing',
-    'wildside'              => 'Wildside Press',
-    'wildside press'        => 'Wildside Press',
+    'a.s.s.t.r'				=> 'ASSTR',
+    'a.s.s.t.r.'			=> 'ASSTR',
+    'ace books'             		=> 'Ace Books',
+    'ace'                   		=> 'Ace Books',
+    'acebooks'              		=> 'Ace Books',
+    'alt sex stories text repository' 	=> 'ASSTR',
+    'alt.sex.stories text repository' 	=> 'ASSTR',
+    'alt.sex.stories'			=> 'ASSTR',
+    'alt.sex.stories.moderated'		=> 'ASSTR',
+    'anchor books'			=> 'Anchor Books',
+    'anchor books a division of random house, inc.' => 'Anchor Books',
+    'anchor booksa division of random house, inc.' => 'Anchor Books',
+    'asstr'				=> 'ASSTR',
+    'baen books'               		=> 'Baen Publishing Enterprises',
+    'baen publishing'       		=> 'Baen Publishing Enterprises',
+    'baen'                  		=> 'Baen Publishing Enterprises',
+    'ballantine books'      		=> 'Ballantine Books',
+    'ballantine'            		=> 'Ballantine Books',
+    'barnes & noble'      		=> 'Barnes and Noble Publishing',
+    'barnes &amp; noble'      		=> 'Barnes and Noble Publishing',
+    'barnes and noble'      		=> 'Barnes and Noble Publishing',
+    'barnes&amp;noble'      		=> 'Barnes and Noble Publishing',
+    'barnes&noble'      		=> 'Barnes and Noble Publishing',
+    'barnesandnoble.com'    		=> 'Barnes and Noble Publishing',
+    'blackmask online'			=> 'Blackmask Online',
+    'cpan'                  		=> 'CPAN',
+    'del rey books'         		=> 'Del Rey Books',
+    'del rey'               		=> 'Del Rey Books',
+    'delrey'                		=> 'Del Rey Books',
+    'e-reads'               		=> 'E-Reads',
+    'electronic text center. university of virginia library.' => 'University of Virginia Library',
+    'ereads'                		=> 'E-Reads',
+    'ereads.com'            		=> 'E-Reads',
+    'feedbooks (www.feedbooks.com)' 	=> 'Feedbooks',
+    'feedbooks'             		=> 'Feedbooks',
+    'fictionwise'           		=> 'Fictionwise',
+    'fictionwise, inc.'       		=> 'Fictionwise',
+    'fictionwise.com'       		=> 'Fictionwise',
+    'gutenberg'             		=> 'Project Gutenberg',
+    'gutenberg.org'         		=> 'Project Gutenberg',
+    'harmony books'         		=> 'Harmony Books',
+    'harmony'               		=> 'Harmony Books',
+    'harper collins'        		=> 'HarperCollins',
+    'harper-collins'        		=> 'HarperCollins',
+    'harpercollins'         		=> 'HarperCollins',
+    'http://www.blackmask.com'		=> 'Blackmask Online',
+    'http://www.blackmask.com/'		=> 'Blackmask Online',
+    'manybooks'             		=> 'ManyBooks',
+    'manybooks.net'         		=> 'ManyBooks',
+    'marion zimmer bradley literary works trust' => 'Marion Zimmer Bradley Literary Works Trust',
+    'mzb literary works trust'		=> 'Marion Zimmer Bradley Literary Works Trust',
+    'penguin group'         		=> 'Penguin Group',
+    'penguin group, usa'    		=> 'Penguin Group',
+    'penguin'               		=> 'Penguin Group',
+    'project gutenberg'     		=> 'Project Gutenberg',
+    'random-house.com'      		=> 'Random House',
+    'randomhouse'           		=> 'Random House',
+    'randomhouse.co.uk'     		=> 'Random House',
+    'rosetta books'         		=> 'Rosetta Books',
+    'rosetta'               		=> 'Rosetta Books',
+    'rosettabooks'          		=> 'Rosetta Books',
+    'siren publishing'      		=> 'Siren Publishing',
+    'siren'                 		=> 'Siren Publishing',
+    'smashwords'			=> 'Smashwords',
+    'smashwords, inc.'			=> 'Smashwords',
+    'stonehouse press'			=> 'Stonehouse Press',
+    'stories online'			=> 'World Literature Company',
+    'storiesonline'			=> 'World Literature Company',
+    'the random house publishing group' => 'Random House',
+    'tom doherty associates'		=> 'Tom Doherty Associates',
+    'university of virginia library'	=> 'University of Virginia Library',
+    'university of virginia library.'	=> 'University of Virginia Library',
+    'wildside press'        		=> 'Wildside Press',
+    'wildside'              		=> 'Wildside Press',
+    'world literature company'          => 'World Literature Company',
+    'www.blackmask.com'			=> 'Blackmask Online',
+    'www.ereads.com'        		=> 'E-Reads',
+    'www.feedbooks.com'     		=> 'Feedbooks',
+    'www.fictionwise.com'   		=> 'Fictionwise',
+    'www.gutenberg.org'     		=> 'Project Gutenberg',
+    'www.random-house.com'  		=> 'Random House',
+    'www.randomhouse.co.uk' 		=> 'Random House',
     );
 
 
@@ -392,6 +4028,35 @@ delete($nonxmlentity2char{'lt'});
 delete($nonxmlentity2char{'quot'});
 delete($nonxmlentity2char{'apos'});
 
+our %referencetypes = (
+    # standard types
+    'acknowledgements'   => 'acknowledgements',
+    'bibliography'       => 'bibliography',
+    'colophon'           => 'colophon',
+    'copyright-page'     => 'copyright-page',
+    'cover'              => 'cover',
+    'dedication'         => 'dedication',
+    'epigraph'           => 'epigraph',
+    'foreword'           => 'foreword',
+    'glossary'           => 'glossary',
+    'index'              => 'index',
+    'loi'                => 'loi',
+    'lot'                => 'lot',
+    'notes'              => 'notes',
+    'preface'            => 'preface',
+    'text'               => 'text',
+    'title-page'         => 'title-page',
+    'toc'                => 'toc',
+    # common nonstandard types
+    'start'              => 'text',
+    'coverimage'         => 'other.ms-coverimage',
+    'coverimagestandard' => 'other.ms-coverimage-standard',
+    'other.copyright'    => 'copyright-page',
+    'other.ms-firstpage' => 'text',
+    'thumbimage'         => 'other.ms-thumbimage',
+    'thumbimagestandard' => 'other.ms-thumbimage-standard',
+   );
+
 our %relatorcodes = (
     'act' => 'Actor',
     'adp' => 'Adapter',
@@ -524,6 +4189,380 @@ our %relatorcodes = (
     'wit' => 'Witness',
     );
 
+our %sexcodes = (
+    # Age/Gender
+    'MF'      => 'Ma/Fa',	# Adult Male over 18, Adult Female over 18
+    'mf'      => 'mt/ft',	# Teen Male under 18, Teen Female under 18
+    'Mf'      => 'Ma/ft',	# Adult Male over 18, Teen Female under 18
+    'mF'      => 'mt/Fa',	# Teen Male under 18, Adult Female
+    'FF'      => 'Fa/Fa',	# 2 or more Females over 18
+    'ff'      => 'ft/ft',	# 2 or more Teen Females under 18
+    'Ff'      => 'Fa/ft',	# Adult Female, Teen female under 18
+    'boy'     => 'boy',		# Boy 12 years old or younger
+    'Boy'     => 'boy',
+    'girl'    => 'gi',		# Girl 12 years old or younger
+    'Girl'    => 'gi',
+    'MM'      => 'Ma/Ma',	# 2 or more Adult Males over 18
+    'Mm'      => 'Ma/mt',	# Adult Male, Teen Male under 18
+    'mm'      => 'mt/mt',	# 2 or more Teen Males under 18
+    'mult'    => 'Mult',	# Multiple Partner. ie MFF or mmmF
+    'teen'    => 'Teenagers',	# Cast of story is mostly teenagers.
+    'Teen'    => 'Teenagers',
+    'teenagers' => 'Teenagers',
+
+    # Level of Consent
+    'cons'       => 'Consensual', # All parties are consenting to the act
+    'consensual' => 'Consensual',
+    'rom'      => 'Romantic',	  #  Mushy love story
+    'romance'  => 'Romantic',
+    'romantic' => 'Romantic',
+    'non-con'  => 'NonConsensual', # At least one of the parties is not participating willfully
+    'nonconsensual' => 'NonConsensual',
+    'pedo'     => 'Pedophilia',	  # An adult initiating sexual contact with a
+				  # pre-pubescent child (boy or girl, 12 or under)
+    'pedophilia' => 'Pedophilia',
+    'lolita'  => 'Lolita',	  # A pre-pubescent child (boy or girl, 12 or
+				  # younger) initiates sexual contact with an adult
+    'reluc'   => 'Reluctant',	  # Start as non consentual and then the non
+				  # consenting party participates willingly
+    'reluctant' => 'Reluctant',
+    'rape'    => 'Rape',	  # Stories with violent rape
+    'coer'    => 'Coercion',	  # Not exactly blackmail, but applying different
+				  # kinds of pressure to force into the act.
+    'coercion' => 'Coercion',
+    'Blkm'    => 'Blackmail',	  # Blackmailing somebody to force them into the sex act depicted
+    'blackmail' => 'Blackmail',
+    'mc'      => 'Mind Control',  # Stories where one party somehow controls another's mind
+    'mind control' => 'Mind Control',
+    'MindControl'  => 'Mind Control',
+    'Mindcontrol'  => 'Mind Control',
+    'mindcontrol'  => 'Mind Control',
+    'hypno'   => 'Hypnosis',	  # One party using hypnosis for sexual purposes
+    'hypnosis' => 'Hypnosis',
+    'dru'     => 'Drunk/Drugged', # One party at least is drunk or drugged and
+				  # participates while under the influence without really knowing
+    'drunk/drugged' => 'Drunk/Drugged',
+    'Drunk'         => 'Drunk/Drugged',
+    'drunk'         => 'Drunk/Drugged',
+    'Drugged'       => 'Drunk/Drugged',
+    'drugged'       => 'Drunk/Drugged',
+    'mag'     => 'Magic', 	  # Contains magic and supernatural phenomenons
+    'slave'   => 'Slavery',	  # Slavery, sexual or otherwise
+    'Slave'   => 'Slavery',
+    'slavery' => 'Slavery',
+
+    # Sexual Orientations
+    'gay'     => 'Gay',		  # Self Explanatory
+    'les'     => 'Lesbian',	  # Self Explanatory
+    'Les'     => 'Lesbian',
+    'lesbian' => 'Lesbian',
+    'lesbian sex' => 'Lesbian',
+    'bi'      => 'BiSexual',	  # Self Explanatory
+    'Bisexual' => 'BiSexual',
+    'bisexual' => 'BiSexual',
+    'het'     => 'Heterosexual',  # Self Explanatory
+    'hetero'  => 'Heterosexual',
+    'heterosexual' => 'Heterosexual',
+    'tg'      => 'TransGender',   # Gender crossing (note that code 'trans' refers to transformation)
+    'Transgender' => 'TransGender',
+    'transgender' => 'TransGender',
+    'cd'      => 'CrossDressing', # Men dressing as women and in women clothing, also vice-versa
+    'Crossdressing' => 'CrossDressing',
+    'crossdressing' => 'CrossDressing',
+    'Crossdress' => 'CrossDressing',
+    'crossdress' => 'CrossDressing',
+    'herm'    => 'Hermaphrodite', # Person having both male and female genitalia
+    'hermaphrodite' => 'Hermaphrodite',
+    'shem'    => 'Shemale',	  # Trans women with male genitalia and female
+    'shemale' => 'Shemale',       # breasts from breast augmentation and/or use of hormones
+
+    # Story Types
+    'fict'     => 'Fiction',	# Fictitious story, not true
+    'fiction'  => 'Fiction',
+    'true'    => 'True Story',	# A true story, according to the author.
+    'hiFant'  => 'High Fantasy', # High Fantasy, sword and sorcery and such
+    'fanfic'  => 'Fan Fiction', # A story using characters from movies, TV shows or Comics
+    'celeb'   => 'Celebrity',	# A story about a real life celebrity, the
+				# story is fictional of course.
+    'ScFi'    => 'Science Fiction', # Science Fiction
+    'Scifi'   => 'Science Fiction',
+    'scifi'   => 'Science Fiction',
+    'TimeTr'  => 'Time Travel',	# Science Fiction involving travel through time
+    'robot'   => 'Robot',	# Humans having relations, sexual or otherwise,
+				# with non-biological entities (mechanical
+				# robots or full blown androids)
+    'PostApoc' => 'Post Apocalypse', # Post Apocalyptic world.
+    'hist'    => 'Historical',	# Story that is set in past history, mostly
+				# related to famous people and events.
+    'horror'  => 'Horror',	# Horror Elements in the story, erotic or otherwise.
+    'humor'   => 'Humor',	# Contains humor or the story is a spoof
+    'tears'   => 'Tear Jerker', # Sad story or containing sad moments
+    'superhero' => 'Superhero', # Story contains Super Hero/Heroine
+    'ESP'     => 'Extra Sensory Perception', # Perceiving things by means other
+				# than the known senses, e.g. by telepathy or clairvoyance.
+    'Zoo'     => 'Zoophilia ',	# Different from bestiality, it's about
+				# relationships between humans and animals, not just sex.
+    'space'   => 'Space',	# Story set in outer space
+    'myst'    => 'Mystery',	# A suspensful story about a mystery
+    'sport'   => 'Sports',	# A story with a sports theme
+    'west'    => 'Western',	# Set in the old US west
+    'doover'  => 'DoOver',	# Getting to do one's life all over again
+
+    # Paranormal
+    'paranormal' => 'Paranormal', # Ghosts, angels, spirits, poltergeists, etc
+    'vamp'    => 'Vampires',	# Vampires in the story
+    'vampire' => 'Vampires',
+    'vampires' => 'Vampires',
+    'furry'   => 'Furry',	# Anthropomorphic animals or 'taurs' (animal
+				# bodies with anthropomorphic torsos) it also
+				# includes other forms such as dragons.
+    'non-anthro' => 'non-anthro', # Animal in form with human level
+				# intelligence, e.g. Werewolves
+    'were'    => 'Were animal', # A human that can turn into an animal temporarily.
+    'zom'     => 'Zombies',	# Zombies in the story
+
+    # Couples
+    'cheat'   => 'Cheating',	# One party is cheating on their spouse
+    'cheating' => 'Cheating',
+    'wife'    => 'Slut Wife',	# Slutty wife
+    'wimp'    => 'Wimp Husband', # Husband does not stand up for himself when
+				# faced with a cheating wife
+    'cuckold' => 'Cuckold',	# When man is being forced to accept
+				# his wife's cheating against his will.
+    'watch'   => 'Wife Watching', # Man enjoys watching his wife getting fucked
+    'reven'   => 'Revenge',	# One party gets revenge for spouse's cheating
+
+    # Incest
+    'inc'     => 'Incest',	# sex between the members of the same family
+    'incest'  => 'Incest',
+    'mother'  => 'Mother',	# Self Explanatory
+    'son'     => 'Son',		# Self Explanatory
+    'bro'     => 'Brother',	# Self Explanatory
+    'brother' => 'Brother',
+    'sis'     => 'Sister',	# Self Explanatory
+    'fath'    => 'Father',	# Self Explanatory
+    'father'  => 'Father',
+    'dau'     => 'Daughter',	# Self Explanatory
+    'daughter' => 'Daughter',
+    'cous'    => 'Cousins',	# Self Explanatory
+    'cousin'  => 'Cousins',
+    'cousins' => 'Cousins',
+    'grand'   => 'Grand Parent', # Self Explanatory
+    'Grandfather' => 'Grand Parent',
+    'grandfather' => 'Grand Parent',
+    'Grandmother' => 'Grand Parent',
+    'grandmother' => 'Grand Parent',
+    'Grandparent' => 'Grand Parent',
+    'grandparent' => 'Grand Parent',
+    'grand parent' => 'Grand Parent',
+    'unc'     => 'Uncle',	# Self Explanatory
+    'uncle'   => 'Uncle',
+    'niece'   => 'Niece',	# Self Explanatory
+    'aunt'    => 'Aunt',	# Self Explanatory
+    'neph'    => 'Nephew',	# Self Explanatory
+    'nephew'  => 'Nephew',
+    'in-law'  => 'InLaws',	# Self Explanatory
+    'inlaw'   => 'InLaws',
+    'inlaws'   => 'InLaws',
+    'Inlaws'   => 'InLaws',
+
+    # BDSM Elements
+    'BDSM'    => 'BDSM',	# Bondage and/or SadoMasochism
+    'bdsm'    => 'BDSM',
+    'bd/sm'   => 'BDSM',
+    'Bondage' => 'BDSM',
+    'bondage' => 'BDSM',
+    'Sadomasochism' => 'BDSM',
+    'sadomasochism' => 'BDSM',
+    'D/S'     => 'DomSub',	# Story about domination, being sexual or otherwise
+    'd/s'     => 'DomSub',
+    'Domsub'  => 'DomSub',
+    'domsub'  => 'DomSub',
+    'Domination' => 'DomSub',
+    'domination' => 'DomSub',
+    'Lesdom'  => 'DomSub',
+    'lesdom'  => 'DomSub',
+    'Mdom'    => 'MaleDom',	# Male Dominant
+    'mdom'    => 'MaleDom',
+    'Fdom'    => 'FemaleDom',	# Female Dominant
+    'fdom'    => 'FemaleDom',
+    'span'    => 'Spanking',	# Somebody gets spanked in the story, willingly or unwillingly
+    'spank'   => 'Spanking',
+    'spanking' => 'Spanking',
+    'rough'   => 'Rough',	# Rough sex
+    'lght'    => 'Light Bond',  # Light Bondage, usually consensual and for
+				# experimentation at the request of the one of the parties
+    'humil'   => 'Humiliation', # Degradation of somebody in the story and/or
+    'Humil'   => 'Humiliation',	# humiliating them publicly
+    'sad'     => 'Sadistic',	# Somebody inflicting pain, physical or mental, for the hell of it
+    'sadism'  => 'Sadistic',
+    'Sadism'  => 'Sadistic',
+    'tort'    => 'Torture',	# Self Explanatory
+    'torture' => 'Torture',
+    'snuff'   => 'Snuff',	# Killing being done during the act of sex.
+				# (Non-sexual murder is in violent)
+
+    # Groups
+    'swing'   => 'Swinging',	# Trading Sexual Partners
+    'swingers' => 'Swinging',
+    'swinging' => 'Swinging',
+    'gang'    => 'Gang Bang',   # Multiple men fucking same woman
+    'GangBang' => 'Gang Bang',
+    'Gangbang' => 'Gang Bang',
+    'gangbang' => 'Gang Bang',
+    'group'   => 'Group Sex',   # Multiple couples in the same place or a threesome
+    'orgy'    => 'Orgy',	# Everybody is Fucking everybody in a group
+    'harem'   => 'Harem',	# There is a harem involved in the story.
+    'poly'    => 'Polygamy/Polyamory', # Multiple spouses or partners (multiple men or multiple women)
+    'polygamy' => 'Polygamy/Polyamory',
+    'Polygamy' => 'Polygamy/Polyamory',
+    'polyamory' => 'Polygamy/Polyamory',
+    'Polyamory' => 'Polygamy/Polyamory',
+
+    # Interracial Elements
+    'interr'  => 'Interracial',   # Sexual partners of different races
+    'Interr'  => 'Interracial',
+    'interrracial'  => 'Interracial',
+    'WC'      => 'White Couple',  # Self Explanatory
+    'BC'      => 'Black Couple',  # Self Explanatory
+    'BF'      => 'Black Female',  # Self Explanatory
+    'BM'      => 'Black Male',    # Self Explanatory
+    'WM'      => 'White Male',    # Self Explanatory
+    'WF'      => 'White Female',  # Self Explanatory
+    'OM'      => 'Oriental Male', # Self Explanatory
+    'OF'      => 'Oriental Female', # Self Explanatory
+    'HM'      => 'Hispanic Male', # Self Explanatory
+    'HF'      => 'Hispanic Female', # Self Explanatory
+
+    # Sexual Activities
+    '1st'     => 'First',	# One of the parties having sex is a virgin
+    'first'   => 'First',	# One of the parties having sex is a virgin
+    'safe'    => 'Safe Sex',    # Sex with proper protection
+    'Safe'    => 'Safe Sex',
+    'oral'    => 'Oral Sex',    # Self Explanatory
+    'oral sex' => 'Oral Sex',
+    'fellatio' => 'Oral Sex',
+    'cunnilingus' => 'Oral Sex',
+    'anal'    => 'Anal Sex',    # Self Explanatory
+    'mastrb'  => 'Masturbation', # Self pleasuring, could be alone or while
+				# with somebody else but no intercourse
+    'masturbation' => 'Masturbation',
+    'pett'    => 'Petting',     # Feeling up and such
+    'petting' => 'Petting',
+    'fingering' => 'Petting',
+    'fist'    => 'Fisting',	# Hand and arm or foot insertion
+    'Fist'    => 'Fisting',
+    'toys'    => 'Sex Toys',	# Sex with the help of sex toys, such as dildos and vibrators
+    'Toys'    => 'Sex Toys',
+    'beast'   => 'Bestiality',  # Sex with animals
+    'bestiality' => 'Bestiality',
+    'squirt'  => 'Squirting',	# Female Ejaculation
+    'squirting' => 'Squirting',
+    'food'    => 'Food',        # Sex where food is involved, like doing it to
+				# a pie or shoving a carrot
+    'lac'     => 'Lactation',   # Drinking and playing with human milk
+    'lact'    => 'Lactation',
+    'lactation' => 'Lactation',
+    'ws'      => 'Water Sports', # Peeing on each other
+    'golden shower' => 'Water Sports',
+    'enem'    => 'Enema',       # Getting an enema as a sexual thing
+    'scat'    => 'Scatology',   # Playing with feces
+    'preg'    => 'Pregnancy',   # Somebody gets pregnant in the story
+    'Preg'    => 'Pregnancy',
+    'creampie' => 'Cream Pie',  # Somebody licks a pussy full of cum
+    'necro'   => 'Necrophilia', # Sex with a corpse
+    'Necro'   => 'Necrophilia',
+    'fart'    => 'Flatulence',  # Flatulence as a sexual fetish
+    'spit'    => 'Spitting',    # Spitting as a sexual fetish
+    'exhib'   => 'Exhibitionism', # Enjoyment of self exposure
+    'Exhib'   => 'Exhibitionism',
+    'voy'     => 'Voyeurism',   # Enjoyment of watching other people without their knowledge
+    'Voy'     => 'Voyeurism',
+    'voyeur'  => 'Voyeurism',
+    'Voyeur'  => 'Voyeurism',
+    'DP'      => 'Double Penetration', # Simultaneous Anal and Vaginal Penetration
+    'dp'      => 'Double Penetration',
+
+    # Fetishes
+    'size'    => 'Size',        # Where the size of the sex parts (penis,
+				# breasts, toys) plays a major role in the story
+    'doct'    => 'Doctor/Nurse', # Medical Fetish, Doctor or Nurse Play a prominent role in the story
+    'BBW'     => 'BBW',		# Big Beautiful Woman. Loving Big Women
+    'feet'    => 'Foot Fetish', # Foot fetish
+    'legs'    => 'Leg Fetish',  # Leg fetish, often with stockings, garters and heels.
+    'hairy'   => 'Hairy Fetish', # with hairy men/women
+    'mod'     => 'Body Modification', # Erotic body modification, including
+				# piercings and tattoos, plays a prominent role.
+    'amput'   => 'Amputee',	# Sex with an amputee
+    'needles' => 'Needles',	# Includes injections and play or permanent piercings.
+    'teach'   => 'Teacher/Student', # Teacher/student relations
+    'teacher/student' => 'Teacher/Student',
+    'teacher student' => 'Teacher/Student',
+    'bbsit'   => 'Babysitter',	# Sex with babysitters fetish
+    'BBr'     => 'Big Breasts', # Women with Big Breasts
+    'clergy'  => 'Clergy',      # Involving members of the clergy (Priest, Bishops, nuns)
+    'menses'  => 'Menstrual Play', # Sex play with a woman who's on menstrual cycle
+    'Menses'  => 'Menstrual Play',
+    'menstruation' => 'Menstrual Play',
+    'Menstruation' => 'Menstrual Play',
+    'public'  => 'Public Sex',	# Sexual events taking place in public
+    'Public'  => 'Public Sex',
+
+    # Other
+    'slow'    => 'Slow',	# There is a story and plot development before
+				# any sex occurs. Not a stroke story.
+    'clas'    => 'Novel-Classic', # An old classic novel that is now in the public domain
+    'bknk'    => 'Novel-Pocketbook', # An old published pocketbook novel that
+				# is not in circulation any more and the
+				# publisher has gone under, so it's not going to be published again
+    'caution' => 'Caution',	# Story contains something that can't be
+				# classified but requires caution on the reader's part
+    '2nd'     => '2nd POV',	# Story written from the 2nd person POV
+    'violent' => 'Violent',	# Violence in the story, not necessarily of sexual nature
+    'work'    => 'Workplace',   # Story centres around a workplace setting
+    'workplace' => 'Workplace',
+    'sch'     => 'School',	# Mostly in an educational setting (high-school, college, University)
+    'school'  => 'School',
+    'schoolgirl' => 'School',
+    'student' => 'School',
+    'college' => 'School',
+    'professor' => 'School',
+    'university' => 'School',
+    'secret'  => 'Secret',      # Secret or hidden or forbidden relationship
+    'Secret'  => 'Secret',
+    'Secret Love' => 'Secret',
+    'Secret love' => 'Secret',
+    'secret love' => 'Secret',
+    'Forbidden Love' => 'Secret',
+    'Forbidden love' => 'Secret',
+    'forbidden love' => 'Secret',
+    'trans'   => 'Transformation', # Un-natural physical transformation
+    'prost'   => 'Prostitution', # Prostitution elements
+    'prostitute' => 'Prostitution',
+    'prostitution' => 'Prostitution',
+    'nud'     => 'Nudism',      # Features scenes set in a naturist/nudist environment
+    'nudism'  => 'Nudism',
+    'nudist'  => 'Nudism',
+    'Mil'     => 'Military',    # Story in a Military setting
+    'military' => 'Military',
+    'pornThea' => 'Porn Theatre', # Story has events in a porn theatre or Adult bookstore
+    'cf'      => 'Cat-Fighting', # Women having a cat fight
+    'catfight' => 'Cat-Fighting',
+    'cannib'  => 'Cannibalism',	# Self explanatory
+    'cannibalism' => 'Cannibalism',
+);
+hashvalue_key_self(\%sexcodes);
+
+our %strangenames = (
+    'brian d foy'	=> 'brian d foy',
+    'brian d. foy'	=> 'brian d foy',
+   );
+
+our %strangefileas = (
+    'foy, brian d'	=> 'd foy, brian',
+    'foy, brian d.'	=> 'd foy, brian',
+   );
+
 our %validspecs = (
     'OEB12' => 'OEB12',
     'OPF20' => 'OPF20',
@@ -536,11 +4575,15 @@ our %validspecs = (
 #####################################################
 
 my %rwfields = (
-    'opffile'  => 'string',
-    'spec'     => 'string',
+    'opffile'   => 'string', # OPF filename (with no path)
+    'opfsubdir' => 'string', # Subdirectory name where opffile is found
+    'erotic'    => 'scalar', # Enable special handling for erotic fiction
+    'spec'      => 'string',
     );
 my %rofields = (
+    'topdir'   => 'string', # Top-level directory of the unpacked book
     'twig'     => 'scalar',
+    'twig_unmodified' => 'scalar',
     'twigroot' => 'scalar',
     'errors'   => 'arrayref',
     'warnings' => 'arrayref',
@@ -565,18 +4608,16 @@ it will also immediately initialize itself via the C<init> method.
 
 =cut
 
-sub new   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
+sub new {
+    my ($self,$filename) = @_;
     my $class = ref($self) || $self;
-    my ($filename) = @_;
     my $subname = (caller(0))[3];
     debug(2,"DEBUG[",$subname,"]");
+    my $bisg = EBook::Tools::BISG->new();
 
     $self = fields::new($class);
 
-    if($filename)
-    {
+    if($filename) {
 	debug(2,"DEBUG: new object from '",$filename,"'");
 	$self->init($filename);
     }
@@ -600,16 +4641,16 @@ initialization croaks.
 
 =cut
 
-sub init :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($filename) = @_;
+sub init :method {
+    my ($self,$filename) = @_;
     my $fh_opffile;
     my $opfstring;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
 
+    $self->{topdir} = getcwd();
+
     if($filename) { $self->{opffile} = $filename; }
 
     if(!$self->{opffile})
@@ -634,7 +4675,15 @@ sub init :method    ## no critic (Always unpack @_ first)
 	croak("OPF file '",$self->{opffile},"' has zero size!");
     }
 
-    debug(2,"DEBUG: init using '",$self->{opffile},"'");
+    # At this point, we have definitely found an OPF file to work
+    # with, but it might not be in the top level directory, and with
+    # the exception of final book construction and EPUB metadata, we
+    # want to be in the directory of the OPF.
+    $self->{opfsubdir} = dirname($self->{opffile});
+    $self->{opffile} = basename($self->{opffile});
+    usedir($self->opfdir);
+
+    debug(2,"DEBUG: init using '",$self->{opfsubdir},"/",$self->{opffile},"'");
 
     # Initialize the twig before use
     $self->{twig} = XML::Twig->new(
@@ -643,8 +4692,15 @@ sub init :method    ## no critic (Always unpack @_ first)
 	pretty_print => 'record'
 	);
 
+    # Initialize a second copy to be used to check for changes
+    $self->{twig_unmodified} = XML::Twig->new(
+	keep_atts_order => 1,
+	output_encoding => 'utf-8',
+	pretty_print => 'record'
+	);
+
     # Read and decode entities before parsing to avoid parsing errors
-    open($fh_opffile,'<:utf8',$self->{opffile})
+    open($fh_opffile,'<:encoding(:UTF-8)',$self->{opffile})
         or croak($subname,"(): failed to open '",$self->{opffile},
                  "' for reading!");
     read($fh_opffile,$opfstring,-s $self->opffile)
@@ -670,7 +4726,9 @@ sub init :method    ## no critic (Always unpack @_ first)
 #         / decode_entities($1) /gex;
 
     $self->{twig}->parse($opfstring);
+    $self->{twig_unmodified}->parse($opfstring);
     $self->{twigroot} = $self->{twig}->root;
+    $self->opf_namespace;
     $self->twigcheck;
     debug(2,"DEBUG[/",$subname,"]");
     return $self;
@@ -692,7 +4750,7 @@ C<init_blank> takes up to three optional named arguments:
 =item C<opffile>
 
 This specifies the OPF filename to use.  If not specified, defaults to
-the name of the current working directory with ".opf" appended
+'content.opf'
 
 =item C<author>
 
@@ -713,10 +4771,8 @@ specified, defaults to "Unknown Title".
 
 =cut
 
-sub init_blank :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub init_blank :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -727,28 +4783,35 @@ sub init_blank :method    ## no critic (Always unpack @_ first)
         'author' => 1,
         'title' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
 
-    $args{opffile} ||= basename(getcwd) . ".opf";
+    $args{opffile} ||= "content.opf";
 
     my $author = $args{author} || 'Unknown Author';
     my $title = $args{title} || 'Unknown Title';
     my $metadata;
     my $element;
 
+    $self->{topdir} = getcwd();
+    $self->{opfsubdir} = '.';
     $self->{opffile} = $args{opffile};
     $self->{twig} = XML::Twig->new(
 	keep_atts_order => 1,
 	output_encoding => 'utf-8',
 	pretty_print => 'record'
         );
+    $self->{twig_unmodified} = XML::Twig->new(
+	keep_atts_order => 1,
+	output_encoding => 'utf-8',
+	pretty_print => 'record'
+       );
 
     $element = XML::Twig::Elt->new('package');
     $self->{twig}->set_root($element);
+    $self->{twig_unmodified}->set_root($element);
     $self->{twigroot} = $self->{twig}->root;
     $metadata = $self->{twigroot}->insert_new_elt('first_child','metadata');
 
@@ -786,8 +4849,7 @@ exists.  Expected values are 'yes' and undef.
 
 =cut
 
-sub adult :method
-{
+sub adult :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -813,8 +4875,7 @@ In scalar context, returns the first contributor, not the last.
 
 =cut
 
-sub contributor_list :method
-{
+sub contributor_list :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -837,6 +4898,60 @@ sub contributor_list :method
 }
 
 
+=head2 C<coverimage()>
+
+Returns the href to the cover image, or undef if none is found.
+
+Checks the following in order:
+
+=over
+
+=item <reference type="other.ms-coverimage-standard">
+
+=item <EmbeddedCover>
+
+=item <meta name="cover"> (as href)
+
+=item <meta name="cover"> (as item id)
+
+=back
+
+=cut
+
+sub coverimage :method {
+    my ($self) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    $self->twigcheck();
+
+    my $twigroot = $self->{twigroot};
+    my $element;
+    my $href;
+    my $id;
+
+    $element = $twigroot->first_descendant('reference[@type="other.ms-coverimage-standard"]');
+    $href = $element->att('href') if $element;
+    return $href if $href;
+
+    $element = $twigroot->first_descendant('EmbeddedCover');
+    $href = $element->text if $element;
+    return $href if $href;
+
+    $element = $twigroot->first_descendant('meta[@name="cover"]');
+    if($element) {
+        if(-f $element->att('content')) {
+            return $element->att('content');
+        }
+        $id = $element->att('content');
+        $element = $twigroot->first_descendant("item[\@id='${id}']");
+        $href = $element->att('href') if $element;
+        return $href if $href;
+    }
+    return;
+}
+
+
 =head2 C<date_list(%args)>
 
 Returns the text of all dc:date elements (case-insensitive) matching
@@ -863,10 +4978,8 @@ matches either one (i.e. the logic is OR).
 
 =cut
 
-sub date_list :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub date_list :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'id' => 1,
         'event' => 1,
@@ -875,8 +4988,7 @@ sub date_list :method    ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -885,22 +4997,17 @@ sub date_list :method    ## no critic (Always unpack @_ first)
     my @list = ();
     my $id;
     my $scheme;
-    foreach my $el (@elements)
-    {
-        if($args{id})
-        {
+    foreach my $el (@elements) {
+        if($args{id}) {
             $id = $el->att('id') || '';
-            if($id eq $args{id})
-            {
+            if($id eq $args{id}) {
                 push(@list,$el->text);
                 next;
             }
         }
-        if($args{event})
-        {
+        if($args{event}) {
             $scheme = $el->att('opf:event') || $el->att('event') || '';
-            if($scheme eq $args{event})
-            {
+            if($scheme eq $args{event}) {
                 push(@list,$el->text);
                 next;
             }
@@ -920,8 +5027,7 @@ Returns the description of the e-book, if set, or undef otherwise.
 
 =cut
 
-sub description :method
-{
+sub description :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -981,10 +5087,8 @@ logic is OR).
 
 =cut
 
-sub element_list :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub element_list :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'cond' => 1,
         'id' => 1,
@@ -994,13 +5098,11 @@ sub element_list :method    ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
-    unless($args{cond})
-    {
+    unless($args{cond}) {
         $self->add_error($subname,"(): no search condition specified");
         return;
     }
@@ -1009,31 +5111,24 @@ sub element_list :method    ## no critic (Always unpack @_ first)
     my @list = ();
     my $id;
     my $scheme;
-    foreach my $el (@elements)
-    {
-        if($args{id})
-        {
+    foreach my $el (@elements) {
+        if($args{id}) {
             $id = $el->att('id') || '';
-            if($id eq $args{id})
-            {
+            if($id eq $args{id}) {
                 push(@list,$el->text);
                 next;
             }
         }
-        if($args{event})
-        {
+        if($args{event}) {
             $scheme = $el->att('opf:event') || $el->att('event') || '';
-            if($scheme eq $args{event})
-            {
+            if($scheme eq $args{event}) {
                 push(@list,$el->text);
                 next;
             }
         }
-        if($args{scheme})
-        {
+        if($args{scheme}) {
             $scheme = $el->att('opf:scheme') || $el->att('scheme') || '';
-            if($scheme eq $args{scheme})
-            {
+            if($scheme eq $args{scheme}) {
                 push(@list,$el->text);
                 next;
             }
@@ -1053,8 +5148,7 @@ Returns an arrayref containing any generated error messages.
 
 =cut
 
-sub errors :method
-{
+sub errors :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1071,8 +5165,7 @@ if it could not be located.
 
 =cut
 
-sub identifier :method
-{
+sub identifier :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1125,10 +5218,8 @@ matches either one (i.e. the logic is OR).
 
 =cut
 
-sub isbn_list :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub isbn_list :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'id' => 1,
         'scheme' => 1,
@@ -1137,8 +5228,7 @@ sub isbn_list :method    ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -1186,10 +5276,8 @@ matches either one (i.e. the logic is OR).
 
 =cut
 
-sub isbns :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub isbns :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'id' => 1,
         'scheme' => 1,
@@ -1198,8 +5286,7 @@ sub isbns :method    ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -1208,13 +5295,10 @@ sub isbns :method    ## no critic (Always unpack @_ first)
     my @list = ();
     my $id;
     my $scheme;
-    foreach my $el (@elements)
-    {
-        if($args{id})
-        {
+    foreach my $el (@elements) {
+        if($args{id}) {
             $id = $el->att('id') || '';
-            if($id eq $args{id})
-            {
+            if($id eq $args{id}) {
                 push(@list,
                      {
                          'isbn' => $el->text,
@@ -1224,11 +5308,9 @@ sub isbns :method    ## no critic (Always unpack @_ first)
                 next;
             }
         }
-        if($args{scheme})
-        {
+        if($args{scheme}) {
             $scheme = $el->att('opf:scheme') || $el->att('scheme') || '';
-            if($scheme eq $args{scheme})
-            {
+            if($scheme eq $args{scheme}) {
                 push(@list,
                      {
                          'isbn' => $el->text,
@@ -1262,8 +5344,7 @@ In scalar context returns the first match, not the last.
 
 =cut
 
-sub languages :method
-{
+sub languages :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1272,8 +5353,7 @@ sub languages :method
 
     my @retval = ();
     my @elements = $self->{twigroot}->descendants(qr/^dc:language$/ix);
-    foreach my $el (@elements)
-    {
+    foreach my $el (@elements) {
         push(@retval,$el->text) if($el->text);
     }
     return unless(@retval);
@@ -1329,10 +5409,8 @@ L</manifest_hrefs()>, L</spine()>
 
 =cut
 
-sub manifest :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub manifest :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'id' => 1,
         'href' => 1,
@@ -1348,13 +5426,11 @@ sub manifest :method    ## no critic (Always unpack @_ first)
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
 
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
-    if($args{logic})
-    {
+    if($args{logic}) {
         croak($subname,
               "(): logic must be 'and' or 'or' (got '",$args{logic},"')")
             if(!$valid_logic{$args{logic}});
@@ -1419,8 +5495,7 @@ See also: C<manifest()>, C<spine_idrefs()>
 
 =cut
 
-sub manifest_hrefs :method
-{
+sub manifest_hrefs :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1430,18 +5505,20 @@ sub manifest_hrefs :method
     my @items;
     my $href;
     my $manifest;
+    my $mimetype;
     my @retval = ();
 
     $manifest = $self->{twigroot}->first_child('manifest');
     if(! $manifest) { return @retval; }
 
     @items = $manifest->descendants('item');
-    foreach my $item (@items)
-    {
+    foreach my $item (@items) {
 	$href = $item->att('href');
-        debug(3,"DEBUG: '",$href,"' has mime-type '",mimetype($href),"'")
-            if($href);
-	push(@retval,$href) if($href);
+        if($href) {
+            $mimetype = mimetype($href) || "UNKNOWN";
+            debug(3,"DEBUG: '",$href,"' has mime-type '",$mimetype,"'");
+            push(@retval,$href);
+        }
     }
     debug(2,"DEBUG[/",$subname,"]");
     if(wantarray) { return @retval; }
@@ -1449,6 +5526,54 @@ sub manifest_hrefs :method
 }
 
 
+=head2 C<opf_namespace()>
+
+Some OPF generators explicity assign 'opf:' in the gi as a prefix on
+OPF elements.  This makes later parsing more complex and is
+unnecessary, so this is stripped before any parsing takes place.
+
+=cut
+
+sub opf_namespace :method {
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(3,"DEBUG[",$subname,"]");
+
+    my @elements = $self->{twig}->descendants(qr/^opf:/ix);
+    foreach my $el (@elements)
+    {
+        my $gi = $el->gi;
+        $gi =~ s/^opf:(.*)/$1/ix;
+        $el->set_gi($gi);
+    }
+    return;
+}
+
+
+=head2 C<opfdir()>
+
+Returns the full filesystem path to the directory where the OPF
+metadata file will be stored, or undef if either the top-level
+directory or the OPF subdirectory is not found.
+
+=cut
+
+sub opfdir :method {
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(3,"DEBUG[",$subname,"]");
+    return unless($self->{topdir});
+    return unless($self->{opfsubdir});
+
+    if($self->{opfsubdir} eq '.') {
+        return $self->{topdir};
+    }
+    return $self->{topdir} . '/' . $self->{opfsubdir};
+}
+
+
 =head2 C<opffile()>
 
 Returns the name of the file where the OPF metadata will be stored or
@@ -1456,17 +5581,37 @@ undef if no value is found..
 
 =cut
 
-sub opffile :method
-{
+sub opffile :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
-    debug(2,"DEBUG[",$subname,"]");
+    debug(3,"DEBUG[",$subname,"]");
     return unless($self->{opffile});
     return $self->{opffile};
 }
 
 
+=head2 C<opfpath()>
+
+Returns the full filesystem path to the file where the OPF metadata
+will be stored or undef if either the top level directory or the OPF
+filename is not found.
+
+=cut
+
+sub opfpath :method {
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(3,"DEBUG[",$subname,"]");
+    return unless($self->{topdir});
+    return unless($self->{opfsubdir});
+    return unless($self->{opffile});
+
+    return $self->{topdir} . '/' . $self->{opfsubdir} . '/' . $self->{opffile};
+}
+
+
 =head2 C<primary_author()>
 
 Finds the primary author of the book, defined as the first
@@ -1493,8 +5638,7 @@ Uses L</twigelt_is_author()> in the first half of the search.
 
 =cut
 
-sub primary_author :method
-{
+sub primary_author :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1506,9 +5650,14 @@ sub primary_author :method
 
     $element = $twigroot->first_descendant(\&twigelt_is_author);
     $element = $twigroot->first_descendant(qr/dc:creator/ix) if(!$element);
-    carp("##DEBUG: primary_author not found") unless($element->text);
-    return unless($element);
-    return unless($element->text);
+    if(! $element) {
+        carp("## WARNING: no dc:creator elements found!");
+        return;
+    }
+    if(! $element->text) {
+        carp("## WARNING: dc:creator element is empty!");
+        return;
+    }
     $fileas = $element->att('opf:file-as');
     $fileas = $element->att('file-as') unless($fileas);
     if(wantarray) { return ($element->text, $fileas); }
@@ -1522,8 +5671,7 @@ Prints the current list of errors to STDERR.
 
 =cut
 
-sub print_errors :method
-{
+sub print_errors :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1531,15 +5679,13 @@ sub print_errors :method
 
     my $errorref = $self->{errors};
 
-    if(!$self->errors)
-    {
+    if(!$self->errors) {
 	debug(1,"DEBUG: no errors found!");
 	return 1;
     }
 
 
-    foreach my $error (@$errorref)
-    {
+    foreach my $error (@$errorref) {
 	print "ERROR: ",$error,"\n";
     }
     return 1;
@@ -1552,8 +5698,7 @@ Prints the current list of warnings to STDERR.
 
 =cut
 
-sub print_warnings :method
-{
+sub print_warnings :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1561,15 +5706,12 @@ sub print_warnings :method
 
     my $warningref = $self->{warnings};
 
-    if(!$self->warnings)
-    {
+    if(!$self->warnings) {
 	debug(2,"DEBUG: no warnings found!");
 	return 1;
     }
 
-
-    foreach my $warning (@$warningref)
-    {
+    foreach my $warning (@$warningref) {
 	print "WARNING: ",$warning,"\n";
     }
     return 1;
@@ -1582,8 +5724,7 @@ Prints the OPF file to the default filehandle
 
 =cut
 
-sub print_opf :method
-{
+sub print_opf :method {
     my $self = shift;
     my $filehandle = shift;
     my $subname = ( caller(0) )[3];
@@ -1605,8 +5746,7 @@ In scalar context returns the first match, not the last.
 
 =cut
 
-sub publishers :method
-{
+sub publishers :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1615,8 +5755,7 @@ sub publishers :method
 
     my @pubs = ();
     my @elements = $self->{twigroot}->descendants(qr/^dc:publisher$/ix);
-    foreach my $el (@elements)
-    {
+    foreach my $el (@elements) {
         push(@pubs,$el->text) if($el->text);
     }
     return unless(@pubs);
@@ -1637,8 +5776,7 @@ Returns undef if the SRP element is not found.
 
 =cut
 
-sub retailprice :method
-{
+sub retailprice :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1661,8 +5799,7 @@ exists.  Returns undef if one is not found.
 
 =cut
 
-sub review :method
-{
+sub review :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1677,7 +5814,7 @@ sub review :method
 }
 
 
-=head2 C<rights('id' => 'identifier')>
+=head2 C<< rights('id' => 'identifier') >>
 
 Returns a list containing the text of all of dc:rights or
 dc:copyrights (case-insensitive) entries in the e-book, or undef if
@@ -1695,10 +5832,8 @@ included only because some broken Mobipocket books use it.
 
 =cut
 
-sub rights :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub rights :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -1750,8 +5885,7 @@ Returns the ID if a match is found, undef otherwise
 
 =cut
 
-sub search_knownuids :method    ## no critic (Always unpack @_ first)
-{
+sub search_knownuids :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1781,10 +5915,8 @@ Returns the ID if a match is found, undef otherwise.
 
 =cut
 
-sub search_knownuidschemes :method   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($gi) = @_;
+sub search_knownuidschemes :method {
+    my ($self,$gi) = @_;
     if(!$gi) { $gi = 'dc:identifier'; }
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1808,8 +5940,7 @@ sub search_knownuidschemes :method   ## no critic (Always unpack @_ first)
 
     my $retval = undef;
 
-    foreach my $scheme (@knownuidschemes)
-    {
+    foreach my $scheme (@knownuidschemes) {
 	debug(2,"DEBUG: searching for scheme='",$scheme,"'");
 	@elems = $topelement->descendants(
             "dc:identifier[\@opf:scheme=~/$scheme/ix or \@scheme=~/$scheme/ix]"
@@ -1817,13 +5948,10 @@ sub search_knownuidschemes :method   ## no critic (Always unpack @_ first)
         push @elems, $topelement->descendants(
             "dc:Identifier[\@opf:scheme=~/$scheme/ix or \@scheme=~/$scheme/ix]"
             );
-	foreach my $elem (@elems)
-	{
+	foreach my $elem (@elems) {
 	    debug(2,"DEBUG: working on scheme '",$scheme,"'");
-	    if(defined $elem)
-	    {
-		if($scheme eq 'FWID')
-		{
+	    if(defined $elem) {
+		if($scheme eq 'FWID') {
 		    # Fictionwise has a screwy output that sets the ID
 		    # equal to the text.  Fix the ID to just be 'FWID'
 		    debug(1,"DEBUG: fixing FWID");
@@ -1831,8 +5959,7 @@ sub search_knownuidschemes :method   ## no critic (Always unpack @_ first)
 		}
 
 		$id = $elem->id;
-                unless(defined $id)
-                {
+                unless(defined $id) {
 		    debug(1,"DEBUG: assigning ID from scheme '",$scheme,"'");
 		    $id = uc($scheme);
 		    $elem->set_id($id);
@@ -1859,8 +5986,7 @@ until it attempts to enforce it.
 
 =cut
 
-sub spec :method
-{
+sub spec :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1887,8 +6013,7 @@ See also: L</spine_idrefs()>, L</manifest()>
 
 =cut
 
-sub spine :method
-{
+sub spine :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1958,8 +6083,7 @@ See also: L</spine()>, L</manifest_hrefs()>
 
 =cut
 
-sub spine_idrefs :method
-{
+sub spine_idrefs :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -1974,8 +6098,7 @@ sub spine_idrefs :method
     if(! $spine) { return @retval; }
 
     @itemrefs = $spine->children('itemref');
-    foreach my $item (@itemrefs)
-    {
+    foreach my $item (@itemrefs) {
 	$idref = $item->att('idref');
 	push(@retval,$idref) if($idref);
     }
@@ -1993,8 +6116,7 @@ In scalar context, returns the first subject, not the last.
 
 =cut
 
-sub subject_list :method
-{
+sub subject_list :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2007,8 +6129,7 @@ sub subject_list :method
     my @subjects = $twigroot->descendants(qr/dc:subject/ix);
     return unless(@subjects);
 
-    foreach my $subject (@subjects)
-    {
+    foreach my $subject (@subjects) {
         push(@retval,$subject->text) if($subject->text);
     }
     return unless(@retval);
@@ -2025,8 +6146,7 @@ no text, returns an empty string.
 
 =cut
 
-sub title :method
-{
+sub title :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2034,10 +6154,13 @@ sub title :method
     $self->twigcheck;
 
     my $twigroot = $self->{twigroot};
+    my $retval;
 
     my $element = $twigroot->first_descendant(qr/^dc:title$/ix);
     return unless($element);
-    return ($element->text || '');
+    $retval = $element->text || '';
+    $retval = trim($retval);
+    return $retval;
 }
 
 
@@ -2053,8 +6176,7 @@ and the result of any subsequent action is not defined.
 
 =cut
 
-sub twig :method
-{
+sub twig :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2071,8 +6193,7 @@ methods that use twig or twigroot.
 
 =cut
 
-sub twigcheck :method
-{
+sub twigcheck :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2110,8 +6231,7 @@ subsequent action is not defined.
 
 =cut
 
-sub twigroot :method
-{
+sub twigroot :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2126,8 +6246,7 @@ Returns an arrayref containing any generated warning messages.
 
 =cut
 
-sub warnings :method
-{
+sub warnings :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2185,10 +6304,8 @@ autodetect the mime type, and if that fails, will default to
 
 =cut
 
-sub add_document :method   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($href,$id,$mediatype) = @_;
+sub add_document :method {
+    my ($self,$href,$id,$mediatype) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -2254,10 +6371,8 @@ SEE ALSO: L</add_warning()>, L</clear_errors()>, L</clear_warnerr()>
 
 =cut
 
-sub add_error :method   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (@newerror) = @_;
+sub add_error :method {
+    my ($self,@newerror) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(3,"DEBUG[",$subname,"]");
@@ -2265,8 +6380,7 @@ sub add_error :method   ## no critic (Always unpack @_ first)
     my $currenterrors;
     $currenterrors = $self->{errors} if($self->{errors});
 
-    if(@newerror)
-    {
+    if(@newerror) {
         my $error = join('',@newerror);
 	debug(1,"ERROR: ",$error);
 	push(@$currenterrors,$error);
@@ -2309,10 +6423,8 @@ contained it.
 
 =cut
 
-sub add_identifier :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub add_identifier :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'text' => 1,
         'id' => 1,
@@ -2322,8 +6434,7 @@ sub add_identifier :method    ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -2338,20 +6449,17 @@ sub add_identifier :method    ## no critic (Always unpack @_ first)
     my $newid = $args{id};
     $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);
 
-    if($dcmeta)
-    {
+    if($dcmeta) {
         $element = $dcmeta->insert_new_elt('last_child','dc:Identifier');
         $element->set_att('scheme' => $args{scheme}) if($args{scheme});
     }
-    else
-    {
+    else {
         $element = $meta->insert_new_elt('last_child','dc:identifier');
         $element->set_att('opf:scheme' => $args{scheme}) if($args{scheme});
     }
     $element->set_text($args{text});
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -2400,10 +6508,8 @@ return undef.
 
 =cut
 
-sub add_item :method   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($href,$id,$mediatype) = @_;
+sub add_item :method {
+    my ($self,$href,$id,$mediatype) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -2418,11 +6524,14 @@ sub add_item :method   ## no critic (Always unpack @_ first)
 
     $id = $href unless($id);
     $id =~ s/[^\w.-]//gx; # Delete all nonvalid XML 1.0 namechars
-    $id =~ s/^[.\d -]+//gx; # Delete all nonvalid XML 1.0 namestartchars
+    if($id =~ /^[.\d -]+/x) {
+       # We're starting with a nonvalid XML 1.0 namestartchar, so
+       # prefix with id_
+        $id = 'id_' . $id;
+    }
 
     $element = $twig->first_elt("*[\@id='$id']");
-    if($element)
-    {
+    if($element) {
         $self->add_error(
             $subname . "(): ID '" . $id . "' already exists"
             . " (in a '" . $element->gi ."' tag)"
@@ -2431,8 +6540,7 @@ sub add_item :method   ## no critic (Always unpack @_ first)
         return;
     }
 
-    if(!$mediatype)
-    {
+    if(!$mediatype) {
 	my $mimetype = mimetype($href);
 	if($mimetype) { $mediatype = $mimetype; }
 	else { $mediatype = "application/xhtml+xml"; }
@@ -2451,7 +6559,7 @@ sub add_item :method   ## no critic (Always unpack @_ first)
 	'media-type' => $mediatype
 	);
 
-    debug(2,"DEBUG[/",$subname,"]");
+    debug(3,"DEBUG[/",$subname,"]");
     return 1;
 }
 
@@ -2527,10 +6635,8 @@ This specifies the scheme attribute to set on the element.
 
 =cut
 
-sub add_metadata :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub add_metadata :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(3,"DEBUG[",$subname,"]");
@@ -2543,22 +6649,19 @@ sub add_metadata :method    ## no critic (Always unpack @_ first)
         'role' => 1,
         'scheme' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
 
     my $gi = $args{gi};
-    unless($gi)
-    {
+    unless($gi) {
         $self->add_error($subname,"(): no gi specified");
         return;
     }
 
     my $text = $args{text};
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
@@ -2577,16 +6680,14 @@ sub add_metadata :method    ## no critic (Always unpack @_ first)
     $meta = $self->{twigroot}->first_child('metadata');
     $dcmeta = $meta->first_child('dc-metadata');
     $parent = $parent || $dcmeta || $meta;
-    if($parent->gi eq 'metadata')
-    {
+    if($parent->gi eq 'metadata') {
         %dcatts = (
             'file-as' => 'opf:file-as',
             'role' => 'opf:role',
             'scheme' => 'opf:scheme',
             );
     }
-    else
-    {
+    else {
         %dcatts = (
             'file-as' => 'file-as',
             'role' => 'role',
@@ -2605,8 +6706,7 @@ sub add_metadata :method    ## no critic (Always unpack @_ first)
     $element->set_text($text);
 
     $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' to a '",$element->gi,"'!"
@@ -2661,10 +6761,8 @@ that previously contained it.
 
 =cut
 
-sub add_subject :method     ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub add_subject :method {
+    my ($self,%args) = @_;
     my %valid_args = (
         'text' => 1,
         'id' => 1,
@@ -2675,8 +6773,7 @@ sub add_subject :method     ## no critic (Always unpack @_ first)
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -2691,21 +6788,24 @@ sub add_subject :method     ## no critic (Always unpack @_ first)
     my $newid = $args{id};
     $idelem = $self->{twig}->first_elt("*[\@id='$newid']") if($newid);
 
-    if($dcmeta)
-    {
-        $element = $dcmeta->insert_new_elt('last_child','dc:Subject');
+    if($dcmeta) {
+        $element = $dcmeta->first_child('dc:Subject[string()="' . $args{text} .  '"]');
+        if(! $element) {
+            $element = $dcmeta->insert_new_elt('last_child','dc:Subject');
+        }
         $element->set_att('scheme' => $args{scheme}) if($args{scheme});
     }
-    else
-    {
-        $element = $meta->insert_new_elt('last_child','dc:subject');
+    else {
+        $element = $meta->first_child('dc:subject[string()="' . $args{text} .  '"]');
+        if(! $element) {
+            $element = $meta->insert_new_elt('last_child','dc:subject');
+        }
         $element->set_att('opf:scheme' => $args{scheme}) if($args{scheme});
     }
     $element->set_text($args{text});
     $element->set_att('BASICCode' => $args{basiccode}) if($args{basiccode});
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -2727,10 +6827,8 @@ SEE ALSO: L</add_error()>, L</clear_warnings()>, L</clear_warnerr()>
 
 =cut
 
-sub add_warning :method   ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (@newwarning) = @_;
+sub add_warning :method {
+    my ($self,@newwarning) = @_;
     my $subname = (caller(0))[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(3,"DEBUG[",$subname,"]");
@@ -2738,8 +6836,7 @@ sub add_warning :method   ## no critic (Always unpack @_ first)
     my @currentwarnings;
     @currentwarnings = @{$self->{warnings}} if($self->{warnings});
 
-    if(@newwarning)
-    {
+    if (@newwarning) {
         my $warning = join('',@newwarning);
 	debug(1,"WARNING: ",$warning);
 	push(@currentwarnings,$warning);
@@ -2757,8 +6854,7 @@ Clear the current list of errors
 
 =cut
 
-sub clear_errors :method
-{
+sub clear_errors :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2775,8 +6871,7 @@ Clear both the error and warning lists
 
 =cut
 
-sub clear_warnerr :method
-{
+sub clear_warnerr :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2794,8 +6889,7 @@ Clear the current list of warnings
 
 =cut
 
-sub clear_warnings :method
-{
+sub clear_warnings :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2816,8 +6910,7 @@ mobi2html may that are not used.
 
 =cut
 
-sub delete_meta_filepos :method
-{
+sub delete_meta_filepos :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2825,56 +6918,215 @@ sub delete_meta_filepos :method
     $self->twigcheck();
 
     my @elements = $self->{twigroot}->descendants('metadata[@filepos]');
-    foreach my $el (@elements)
-    {
+    foreach my $el (@elements) {
 	$el->delete;
     }
     return 1;
 }
 
 
-=head2 C<fix_dates()>
+=head2 C<delete_subject(%args)>
 
-Standardizes all <dc:date> elements via fix_datestring().  Adds a
-warning to the object for each date that could not be fixed.
+Deletes dc:subject and dc:Subject elements based on text content or
+the id, scheme, or basiccode attributes.  Matches are case-sensitive.
 
-Called from L</fix_misc()>.
+Specifying multiple arguments will delete subject matching any of them.
+
+This has the same potential arguments as add_subject.
+
+Returns the count of elements deleted.
 
 =cut
 
-sub fix_dates :method
-{
-    my $self = shift;
+sub delete_subject :method {
+    my ($self,%args) = @_;
+    my %valid_args = (
+        'text' => 1,
+        'id' => 1,
+        'scheme' => 1,
+        'basiccode' => 1,
+        );
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     $self->twigcheck();
+    foreach my $arg (keys %args) {
+        croak($subname,"(): invalid argument '",$arg,"'")
+            if(!$valid_args{$arg});
+    }
 
-    my @dates;
-    my $newdate;
+    my @elements;
+    my $count = 0;
 
-    @dates = $self->{twigroot}->descendants('dc:date');
-    push(@dates,$self->{twigroot}->descendants('dc:Date'));
+    if(defined $args{text}) {
+        @elements = $self->{twigroot}->descendants('dc:subject[text()="' . $args{text} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:Subject[text()="' . $args{text} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+    }
 
-    foreach my $dcdate (@dates)
-    {
-	if(!$dcdate->text)
-	{
+    if($args{id}) {
+        @elements = $self->{twigroot}->descendants('dc:subject[@id="' . $args{id} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:Subject[@id="' . $args{id} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+    }
+
+    if($args{scheme}) {
+        @elements = $self->{twigroot}->descendants('dc:subject[@scheme="' . $args{scheme} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:subject[@opf:scheme="' . $args{scheme} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:Subject[@scheme="' . $args{scheme} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:Subject[@opf:scheme="' . $args{scheme} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+    }
+
+    if($args{basiccode}) {
+        @elements = $self->{twigroot}->descendants('dc:subject[@BASICCode="' . $args{id} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+        @elements = $self->{twigroot}->descendants('dc:Subject[@BASICCode="' . $args{id} . '"]');
+        foreach my $el (@elements) {
+            $el->delete;
+            $count++;
+        }
+    }
+    debug(2,"DEBUG: Deleted ",$count," elements");
+    return $count;
+}
+
+
+=head2 C<fix_creators()>
+
+Normalizes creator and contributor names and file-as attributes
+
+Names are normalized to 'First Last' format, while file-as attributes
+are normalized to 'Last, First' format.
+
+This can damage some unusual names that do not match standard
+capitalization formats, so it is not made part of L</fix_misc()>.
+
+=cut
+
+sub fix_creators :method {
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    $self->twigcheck();
+
+    my $twigroot = $self->{twigroot};
+    my @elements = $twigroot->descendants(qr/dc:(creator|contributor)/ix);
+    my $nameparse = Lingua::EN::NameParse->new(
+        allow_reversed  => 1,
+        extended_titles => 1,
+        force_case      => 1,
+        lc_prefix       => 1,
+       );
+    foreach my $el (@elements) {
+        my $fileas = $el->att('opf:file-as') || '';
+        my $name = $el->text || '';
+        my $fixed;
+        $name = trim($name);
+
+        if ( $nameparse->parse($name) ) {
+	    $self->add_warning(
+                "failure while parsing name: ",
+                {$nameparse->properties}->{non_matching});
+        }
+        else {
+            $fixed = $nameparse->case_all;
+            if($fixed and $strangenames{lc $fixed}) {
+                $fixed = $strangenames{lc $fixed};
+            }
+            debug(2,"DEBUG: creator name '",$name,"' -> '",
+                  $fixed,"'");
+            $el->set_text($fixed);
+
+            $fixed = $nameparse->case_all_reversed;
+            if($fixed and $strangefileas{lc $fixed}) {
+                $fixed = $strangefileas{lc $fixed};
+            }
+            if($fixed) {
+                debug(2,"DEBUG: creator file-as '",$fileas,"' -> '",
+                      $fixed,"'");
+                $el->set_att('opf:file-as',$fixed);
+            }
+            else {
+                debug(2,"DEBUG: removing empty creator file-as from creator '",
+                      $name,"'");
+                $el->del_att('opf:file-as');
+            }
+        }
+    }
+    return;
+}
+
+
+=head2 C<fix_dates()>
+
+Standardizes all <dc:date> elements via fix_datestring().  Adds a
+warning to the object for each date that could not be fixed.
+
+Called from L</fix_misc()>.
+
+=cut
+
+sub fix_dates :method {
+    my $self = shift;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    $self->twigcheck();
+
+    my @dates;
+    my $newdate;
+
+    @dates = $self->{twigroot}->descendants('dc:date');
+    push(@dates,$self->{twigroot}->descendants('dc:Date'));
+
+    foreach my $dcdate (@dates) {
+	if(!$dcdate->text) {
 	    $self->add_warning(
                 "WARNING: found dc:date with no value -- skipping");
 	}
-	else
-	{
+	else {
 	    $newdate = fix_datestring($dcdate->text);
-	    if(!$newdate)
-	    {
+	    if(!$newdate) {
 		$self->add_warning(
 		    sprintf("fixmisc(): can't deal with date '%s' -- skipping",
                             $dcdate->text)
 		    );
 	    }
-	    elsif($dcdate->text ne $newdate)
-	    {
+	    elsif($dcdate->text ne $newdate) {
 		debug(2,"DEBUG: setting date from '",$dcdate->text,
                       "' to '",$newdate,"'");
 		$dcdate->set_text($newdate);
@@ -2891,10 +7143,13 @@ Fixes problems related to the OPF guide elements, specifically:
 
 =over
 
-=item * Deletes empty guide elements
+=item * Ensures the guide element exists
 
 =item * Moves all reference elements directly underneath the guide element
 
+=item * Finds nonstandard reference types and either converts them to
+standard or prefaces them with 'other.'
+
 =item * Finds reference elements with a href with only an anchor
 portion and assigns them to the first spine href.  This only works if
 the spine is in working condition, so it may be wise to run
@@ -2908,8 +7163,7 @@ the manifest.
 
 =cut
 
-sub fix_guide :method
-{
+sub fix_guide :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -2920,68 +7174,66 @@ sub fix_guide :method
     my $guide = $twigroot->first_descendant('guide');
     my $parent;
     my $href;
+    my $type;
     my @spine;
+
+    # If <guide> doesn't exist, create it
+    unless($guide) {
+        debug(1,"DEBUG: creating <guide>");
+        $guide = $twigroot->insert_new_elt('last_child','guide');
+    }
+
+    # Make sure that the guide is a child of the twigroot,
+    $parent = $guide->parent;
+    if( $parent->cmp($twigroot) ) {
+        debug(1,"DEBUG: moving <guide>");
+        $guide->move('last_child',$twigroot);
+    }
+
+
     my @elements = $twigroot->descendants(qr/^reference$/ix);
-    if(@elements)
-    {
-        # If <guide> doesn't exist, create it
-        unless($guide)
-        {
-            debug(1,"DEBUG: creating <guide>");
-            $guide = $twigroot->insert_new_elt('last_child','guide');
+    foreach my $el (@elements) {
+        $type = $el->att('type');
+        if($referencetypes{$type}) {
+            $el->set_att('type',$referencetypes{$type});
         }
-
-        # Make sure that the guide is a child of the twigroot,
-        $parent = $guide->parent;
-        if( $parent->cmp($twigroot) )
-        {
-            debug(1,"DEBUG: moving <guide>");
-            $guide->move('last_child',$twigroot);
+        elsif($type !~ /^other./x) {
+            $type = 'other.' . $type;
+            $el->set_att('type',$type);
         }
 
-        foreach my $el (@elements)
-        {
-            $href = $el->att('href');
-            if(!$href)
-            {
-                # No href means it is broken.
-                # Leave it alone, but log a warning
+        $href = $el->att('href');
+        if (!$href) {
+            # No href means it is broken.
+            # Leave it alone, but log a warning
+            $self->add_warning(
+                "fix_guide(): <reference> with no href -- skipping");
+            next;
+        }
+        if ($href =~ /^#/) {
+            # Anchor-only href.  Attempt to fix from the first
+            # spine entry
+            @spine = $self->spine;
+            if (!@spine) {
                 $self->add_warning(
-                    "fix_guide(): <reference> with no href -- skipping");
-                next;
+                    "fix_guide(): Cannot correct reference href '",$href,
+                    "', spine is empty");
             }
-            if($href =~ /^#/)
-            {
-                # Anchor-only href.  Attempt to fix from the first
-                # spine entry
-                @spine = $self->spine;
-                if(!@spine)
-                {
-                    $self->add_warning(
-                        "fix_guide(): Cannot correct reference href '",$href,
-                        "', spine is empty");
-                }
-                elsif(!$spine[0]->{href})
-                {
-                    $self->add_warning(
-                        "fix_guide(): Cannot correct reference href '",$href,
-                        "', cannot find href for first spine entry");
-                }
-                else
-                {
-                    debug(1,"DEBUG: correcting reference href from '",$href,
-                          "' to '",$spine[0]->{href} . $href,"'");
-                    $el->set_att('href',$spine[0]->{href} . $href);
-                }
+            elsif (!$spine[0]->{href}) {
+                $self->add_warning(
+                    "fix_guide(): Cannot correct reference href '",$href,
+                    "', cannot find href for first spine entry");
             }
-            debug(3,"DEBUG: processing reference '",$href,"')");
-            $el->move('last_child',$guide);
-        } # foreach my $el (@elements)
-    } # if(@elements)
-    else # No elements, delete guide if it exists
-    {
-        $guide->delete if($guide);
-    }
+            else {
+                debug(1,"DEBUG: correcting reference href from '",$href,
+                      "' to '",$spine[0]->{href} . $href,"'");
+                $el->set_att('href',$spine[0]->{href} . $href);
+            }
+        }
+        debug(3,"DEBUG: processing reference '",$href,"')");
+        $el->move('last_child',$guide);
+    } # foreach my $el (@elements)
+
     return 1;
 }
 
@@ -3007,10 +7259,8 @@ element.  If not specified, defaults to 'en'.
 
 =cut
 
-sub fix_languages :method
-{
-    my $self = shift;
-    my %args = @_;
+sub fix_languages :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -3019,8 +7269,7 @@ sub fix_languages :method
     my %valid_args = (
         'default' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -3029,17 +7278,14 @@ sub fix_languages :method
     my $defaultlang = $args{default} || 'en';
     my $langel;
     my @elements = $twigroot->descendants(qr/dc:language/ix);
-    while($langel = shift(@elements) )
-    {
-        foreach my $el (@elements)
-        {
+    while($langel = shift(@elements) ) {
+        foreach my $el (@elements) {
             $el->delete if(twigelt_detect_duplicate($el,$langel) );
         }
     }
 
     @elements = $self->languages;
-    if(!@elements)
-    {
+    if(!@elements) {
         $self->set_language(text => $defaultlang);
     }
     return 1;
@@ -3059,8 +7305,7 @@ returns undef.  Otherwise, it returns 1.
 
 =cut
 
-sub fix_links :method
-{
+sub fix_links :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3091,16 +7336,15 @@ sub fix_links :method
         'application/xml' => 1
         );
 
-    if(!$manifest)
-    {
+    if(!$manifest) {
         $self->add_warning(
             "fix_links(): no manifest found!"
             );
         return;
     }
+
     @unchecked = $self->manifest_hrefs;
-    if(!@unchecked)
-    {
+    if(!@unchecked) {
         $self->add_warning(
             "fix_links(): empty manifest found!"
             );
@@ -3109,29 +7353,34 @@ sub fix_links :method
 
     # Initialize %links, so we don't try to add something already in
     # the manifest
-    foreach my $mhref (@unchecked)
-    {
+    foreach my $mhref (@unchecked) {
+        $mhref = uri_unescape($mhref);
         $links{$mhref} = undef unless(exists $links{$mhref});
     }
 
-    while(@unchecked)
-    {
+    while(@unchecked) {
         debug(3,"DEBUG: ",scalar(@unchecked),
               " items left to check at start of loop");
         $href = shift(@unchecked);
         $href = trim($href);
+        $href = uri_unescape($href);
         debug(3,"DEBUG: checking '",$href,"'");
         next if(defined $links{$href});
 
+        # Skip mailto: and news: links
+        if($href =~ m#(mailto|news):#ix) {
+            debug(1,"DEBUG: mailto link '",$href,"' skipped");
+            $links{$href} = 0;
+            next;
+        }
+
         # Skip URIs for now
-        if($href =~ m#^ \w+://#ix)
-        {
+        if($href =~ m#^ \w+://#ix) {
             debug(1,"DEBUG: URI '",$href,"' skipped");
             $links{href} = 0;
             next;
         }
-        if(! -f $href)
-        {
+        if(! -f $href) {
             $self->add_warning(
                 "fix_links(): '" . $href . "' not found"
                 );
@@ -3141,8 +7390,7 @@ sub fix_links :method
 
         $mimetype = mimetype($href);
 
-        if(!$linking_mimetypes{$mimetype})
-        {
+        if(!$linking_mimetypes{$mimetype}) {
             debug(2,"DEBUG: '",$href,"' has mimetype '",$mimetype,
                 "' -- not checking");
             $links{$href} = 1;
@@ -3150,22 +7398,25 @@ sub fix_links :method
         }
 
         debug(1,"DEBUG: finding links in '",$href,"'");
-        push(@newlinks,find_links($href));
+        @newlinks = find_links($href);
         trim(@newlinks) if(@newlinks);
         $links{$href} = 1;
-        foreach my $newlink (@newlinks)
-        {
+        foreach my $newlink (sort @newlinks) {
+            # Skip mailto: and news: links
+            if($newlink =~ m#(mailto|news):#ix) {
+                debug(1,"DEBUG: mailto link '",$href,"' skipped");
+                next;
+            }
             # Skip URIs for now
-            if($newlink =~ m#^ \w+://#ix)
-            {
+            elsif($newlink =~ m#^ \w+://#ix) {
                 debug(1,"DEBUG: URI '",$newlink,"' skipped");
                 next;
             }
-            elsif(!exists $links{$newlink})
-            {
-                debug(2,"DEBUG: adding '",$newlink,"' to the list");
+            elsif(!exists $links{$newlink}) {
+                debug(2,"DEBUG: adding '",$newlink,"' to the manifest");
                 push(@unchecked,$newlink);
                 $self->add_item($newlink);
+                $links{$newlink} = 1;
             }
         }
         debug(2,"DEBUG: ",scalar(@unchecked),
@@ -3189,8 +7440,7 @@ continues.
 
 =cut
 
-sub fix_manifest :method
-{
+sub fix_manifest :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3199,16 +7449,18 @@ sub fix_manifest :method
 
     my $twigroot = $self->{twigroot};
 
+    my $spec = $self->spec || '';
     my $manifest = $twigroot->first_descendant('manifest');
     my @elements;
+    my @extras;
     my $parent;
 
     my $href;
     my $id;
+    my $type;
 
     # If <manifest> doesn't exist, create it
-    if(! $manifest)
-    {
+    if(! $manifest) {
 	debug(1,"DEBUG: creating <manifest>");
 	$manifest = $twigroot->insert_new_elt('last_child','manifest');
     }
@@ -3216,21 +7468,44 @@ sub fix_manifest :method
     # Make sure that the manifest is the first child of the twigroot,
     # which should be <package>
     $parent = $manifest->parent;
-    if($parent != $twigroot)
-    {
+    if($parent != $twigroot) {
 	debug(1,"DEBUG: moving <manifest>");
-	$manifest->move('last_child',$twigroot);
+	$manifest->move('first_child',$twigroot);
+    }
+
+    # Find and merge any other manifests
+    @extras = $twigroot->descendants('manifest');
+    shift @extras;
+    foreach my $extra (@extras) {
+        my @elements = $extra->children;
+        foreach my $el (@elements) {
+            debug(2,"DEBUG: moving <",$el->gi,"> into primary manifest");
+            $el->move('last_child',$manifest);
+        }
+        $extra->delete;
     }
 
     @elements = $twigroot->descendants(qr/^item$/ix);
-    foreach my $el (@elements)
-    {
+    foreach my $el (@elements) {
         $href = $el->att('href');
         $id = $el->id;
-        if(!$id)
-        {
-            if(!$href)
-            {
+        $type = $el->att('media-type');
+
+        # Convert to OPF2.0 document types if necessary
+        if($spec eq 'OPF20') {
+            if($type eq "text/x-oeb1-document") {
+                $type = "application/xhtml+xml";
+                $el->set_att('media-type',$type);
+            }
+            if($id eq 'ncx') {
+                $type = 'application/x-dtbncx+xml';
+                $el->set_att('media-type',$type);
+            }
+
+        }
+
+        if(!$id) {
+            if(!$href) {
                 # No ID, no href, there's something very fishy here,
                 # so log a warning.
                 # If it is already underneath <manifest>, move it to
@@ -3239,12 +7514,10 @@ sub fix_manifest :method
                     "fix_manifest(): found item with no id or href"
                     );
                 debug(1,"fix_manifest(): found item with no id or href");
-                if($el->parent == $manifest)
-                {
+                if($el->parent == $manifest) {
                     $el->move('last_child',$manifest);
                 }
-                else
-                {
+                else {
                     print "DEBUG: skipping item with no id or href\n"
                         if($debug);
                 }
@@ -3258,8 +7531,7 @@ sub fix_manifest :method
             debug(1,"DEBUG: processing item with no id (href='",$href,"')");
             $el->move('last_child',$manifest);
         } # if(!$id)
-        if(!$href)
-        {
+        if(!$href) {
             # We have an ID, but no href.  Log a warning, but move it anyway.
             $self->add_warning(
                 "fix_manifest(): item with id '" . $id . "' has no href!"
@@ -3267,8 +7539,7 @@ sub fix_manifest :method
             debug(1,"fix_manifest(): item with id '",$id,"' has no href!");
             $el->move('last_child',$manifest);
         }
-        else
-        {
+        else {
             # We have an ID and a href
             debug(3,"DEBUG: processing item '",$id,"' (href='",$href,"')");
             $el->move('last_child',$manifest);
@@ -3280,15 +7551,16 @@ sub fix_manifest :method
 =head2 C<fix_metastructure_basic()>
 
 Verifies that <metadata> exists (creating it if necessary), and moves
-it to be the first child of <package>.
+it to be the first child of <package>.  If additional <metadata>
+elements exist, their children are moved into the first one found and
+then the extras are deleted.
 
 Used in L</fix_metastructure_oeb12()>, L</fix_packageid()>, and
 L</set_primary_author(%args)>.
 
 =cut
 
-sub fix_metastructure_basic :method
-{
+sub fix_metastructure_basic :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3298,13 +7570,30 @@ sub fix_metastructure_basic :method
     my $twigroot = $self->{twigroot};
     my $metadata = $twigroot->first_descendant('metadata');
 
-    if(! $metadata)
-    {
+    my @extras = $twigroot->descendants('metadata');
+    shift @extras;
+
+    if(! $metadata) {
 	debug(1,"DEBUG: creating <metadata>");
 	$metadata = $twigroot->insert_new_elt('first_child','metadata');
     }
+
+    foreach my $extra (@extras) {
+        my @elements = $extra->children;
+        foreach my $el (@elements) {
+            debug(2,"DEBUG: moving <",$el->gi,"> into primary metadata");
+            $el->move('last_child',$metadata);
+        }
+        $extra->delete;
+    }
     debug(3,"DEBUG: moving <metadata> to be the first child of <package>");
     $metadata->move('first_child',$twigroot);
+
+    if($metadata->att('xmlns:calibre')) {
+        # xmlns:calibre isn't a real namespace; it's just there for
+        # advertising, so remove it
+        $metadata->del_att('xmlns:calibre');
+    }
     return 1;
 }
 
@@ -3320,8 +7609,7 @@ Used in L</fix_oeb12()> and L</fix_mobi()>.
 
 =cut
 
-sub fix_metastructure_oeb12 :method
-{
+sub fix_metastructure_oeb12 :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3341,32 +7629,28 @@ sub fix_metastructure_oeb12 :method
 
     # If <dc-metadata> doesn't exist, we'll have to create it.
     $dcmeta = $twigroot->first_descendant('dc-metadata');
-    if(! $dcmeta)
-    {
+    if(! $dcmeta) {
 	debug(2,"DEBUG: creating <dc-metadata>");
 	$dcmeta = $metadata->insert_new_elt('first_child','dc-metadata');
     }
 
     # Make sure that $dcmeta is a child of $metadata
     $parent = $dcmeta->parent;
-    if($parent != $metadata)
-    {
+    if($parent != $metadata) {
 	debug(2,"DEBUG: moving <dc-metadata>");
 	$dcmeta->move('first_child',$metadata);
     }
 
     # If <x-metadata> doesn't exist, create it
     $xmeta = $metadata->first_descendant('x-metadata');
-    if(! $xmeta)
-    {
+    if(! $xmeta) {
         debug(2,"DEBUG: creating <x-metadata>");
         $xmeta = $metadata->insert_new_elt('last_child','x-metadata');
     }
 
     # Make sure that x-metadata is a child of metadata
     $parent = $xmeta->parent;
-    if($parent != $metadata)
-    {
+    if($parent != $metadata) {
         debug(2,"DEBUG: moving <x-metadata>");
         $xmeta->move('after',$dcmeta);
     }
@@ -3380,7 +7664,11 @@ Fixes miscellaneous potential problems in OPF data.  Specifically,
 this is a shortcut to calling L</delete_meta_filepos()>,
 L</fix_packageid()>, L</fix_dates()>, L</fix_languages()>,
 L</fix_publisher()>, L</fix_manifest()>, L</fix_spine()>,
-L</fix_guide()>, and L</fix_links()>.
+L</fix_subjects()>, L</fix_type()>, L</fix_guide()>, and
+L</fix_links()>.
+
+L</fix_creators()> is not run from this, as it carries a risk of taking
+a correct name and making it incorrect.
 
 The objective here is that you can run either C<fix_misc()> and either
 L</fix_oeb12()> or L</fix_opf20()> and a perfectly valid OPF file will
@@ -3388,8 +7676,7 @@ result from only two calls.
 
 =cut
 
-sub fix_misc :method
-{
+sub fix_misc :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3403,6 +7690,8 @@ sub fix_misc :method
     $self->fix_publisher();
     $self->fix_manifest();
     $self->fix_spine();
+    $self->fix_subjects();
+    $self->fix_type();
     $self->fix_guide();
     $self->fix_links();
 
@@ -3432,8 +7721,7 @@ become noncompliant with IDPF specifications.
 
 =cut
 
-sub fix_mobi :method
-{
+sub fix_mobi :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3499,49 +7787,42 @@ sub fix_mobi :method
     # If <x-metadata> doesn't exist, create it.  Even if there are no
     # mobi-specific tags, this method will create at least one
     # (<output>) which will need it.
-    if(!$xmeta)
-    {
+    if(!$xmeta) {
         debug(2,"DEBUG: creating <x-metadata>");
         $xmeta = $dcmeta->insert_new_elt('after','x-metadata')
     }
 
-    foreach my $tag (@mobitags)
-    {
+    foreach my $tag (@mobitags) {
         @elements = $twigroot->descendants($tag);
         next unless (@elements);
 
         # In theory, only one Mobipocket-specific element should ever
         # be present in a document.  We'll deal with multiples anyway,
         # but send a warning.
-        if(scalar(@elements) > 1)
-        {
+        if(scalar(@elements) > 1) {
             $self->add_warning(
                 'fix_mobi(): Found ' . scalar(@elements) . " '" . $tag .
                 "' elements, but only one should exist."
                 );
         }
 
-        foreach my $el (@elements)
-        {
+        foreach my $el (@elements) {
             $el->move('last_child',$xmeta);
         }
     }
 
     $output = $xmeta->first_child('output');
-    if($output)
-    {
+    if($output) {
 	my $encoding = $mobiencodings{$output->att('encoding')};
 	my $contenttype = $mobicontenttypes{$output->att('content-type')};
 
-	if($contenttype)
-	{
+	if($contenttype) {
 	    $output->set_att('encoding','utf-8') if(!$encoding);
 	    debug(2,"DEBUG: setting encoding only and returning");
 	    return 1;
 	}
     }
-    else
-    {
+    else {
         debug(1,"DEBUG: creating <output> under <x-metadata>");
         $output = $xmeta->insert_new_elt('last_child','output');
     }
@@ -3583,8 +7864,7 @@ forced to be under <metadata>
 
 =cut
 
-sub fix_oeb12 :method
-{
+sub fix_oeb12 :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3613,11 +7893,9 @@ sub fix_oeb12 :method
 
     # Set the correct tag name and move it into <dc-metadata> in the
     # right order
-    foreach my $dcel (keys %dcelements12)
-    {
+    foreach my $dcel (keys %dcelements12) {
         @elements = $twigroot->descendants(qr/^$dcel$/ix);
-        foreach my $el (@elements)
-        {
+        foreach my $el (@elements) {
             debug(3,"DEBUG: processing '",$el->gi,"'");
             croak("Found invalid DC element '",$el->gi,"'!")
                 if(!$dcelements12{lc $el->gi});
@@ -3630,28 +7908,30 @@ sub fix_oeb12 :method
     # Handle non-DC metadata, deleting <x-metadata> if it isn't
     # needed.
     @elements = $metadata->children(qr/^(?!(?s:.*)-metadata)/x);
-    if(@elements)
-    {
-	if($debug)
-	{
+    if(@elements) {
+	if($debug) {
 	    print {*STDERR} "DEBUG: extra metadata elements found: ";
 	    foreach my $el (@elements) { print {*STDERR} $el->gi," "; }
 	    print {*STDERR} "\n";
 	}
-	foreach my $el (@elements)
-	{
+	foreach my $el (@elements) {
 	    $el->move('last_child',$xmeta);
 	}
     }
     @elements = $twigroot->children(qr/^meta$/ix);
-    foreach my $el (@elements)
-    {
+    foreach my $el (@elements) {
         $el->set_gi(lc $el->gi);
         $el->move('last_child',$xmeta);
     }
     @elements = $xmeta->children;
     $xmeta->delete unless(@elements);
 
+    # Delete Calibre advertisements in dc:Contributor elements
+    @elements = $twigroot->descendants('dc:Contributor[string() =~ /calibre-ebook.com|calibre.kovidgoyal.net/]');
+    foreach my $el (@elements) {
+	$el->delete;
+    }
+
     # Fix <manifest> and <spine>
     $self->fix_manifest;
     $self->fix_spine;
@@ -3687,8 +7967,7 @@ they are.
 
 =cut
 
-sub fix_oeb12_dcmetatags :method
-{
+sub fix_oeb12_dcmetatags :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3699,11 +7978,9 @@ sub fix_oeb12_dcmetatags :method
 
     my @elements;
 
-    foreach my $dcmetatag (keys %dcelements12)
-    {
+    foreach my $dcmetatag (keys %dcelements12) {
 	@elements = $topelement->descendants(qr/^$dcmetatag$/ix);
-	foreach my $el (@elements)
-	{
+	foreach my $el (@elements) {
 	    $el->set_tag($dcelements12{lc $el->tag})
 		if($dcelements12{lc $el->tag});
 	}
@@ -3738,8 +8015,7 @@ dc:rights)
 
 =cut
 
-sub fix_opf20 :method
-{
+sub fix_opf20 :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3749,6 +8025,10 @@ sub fix_opf20 :method
     # Ensure a sane structure
     $self->fix_metastructure_basic();
 
+    # If there is an existing cover image, ensure it hits all standards
+    my $coverimage = $self->coverimage;
+    $self->set_cover('href' => $coverimage) if $coverimage;
+
     my $twigroot = $self->{twigroot};
     my $metadata = $twigroot->first_descendant('metadata');
     my @elements;
@@ -3757,10 +8037,8 @@ sub fix_opf20 :method
     # underneath <metadata> so that its children will collapse to the
     # correct position, then erase it.
     @elements = $twigroot->descendants('dc-metadata');
-    if(@elements)
-    {
-	foreach my $dcmeta (@elements)
-	{
+    if(@elements) {
+	foreach my $dcmeta (@elements) {
 	    debug(1,"DEBUG: moving <dc-metadata>");
 	    $dcmeta->move('first_child',$metadata);
 	    $dcmeta->erase;
@@ -3771,24 +8049,26 @@ sub fix_opf20 :method
     # <metadata> so that its children will collapse to the correct
     # position, then erase it.
     @elements = $twigroot->descendants('x-metadata');
-    if(@elements)
-    {
-	foreach my $xmeta (@elements)
-	{
+    if(@elements) {
+	foreach my $xmeta (@elements) {
 	    debug(1,"DEBUG: moving <x-metadata>");
 	    $xmeta->move('last_child',$metadata);
 	    $xmeta->erase;
 	}
     }
 
+    # Delete any old OEB12 output elements
+    @elements = $twigroot->descendants('output');
+    foreach my $el (@elements) {
+        $el->delete;
+    }
+
     # For all DC elements at any location, set the correct tag name
     # and attribute namespace and move it directly under <metadata>
-    foreach my $dcmetatag (keys %dcelements20)
-    {
+    foreach my $dcmetatag (keys %dcelements20) {
 	@elements = $twigroot->descendants(qr/$dcmetatag/ix);
-	foreach my $el (@elements)
-	{
-	    debug(1,"DEBUG: checking element '",$el->gi,"'");
+	foreach my $el (@elements) {
+	    debug(2,"DEBUG: checking DC element <",$el->gi,">");
 	    $el->set_gi($dcelements20{$dcmetatag});
             $el = twigelt_fix_opf20_atts($el);
 	    $el->move('last_child',$metadata);
@@ -3796,15 +8076,27 @@ sub fix_opf20 :method
     }
 
     # Find any <meta> elements anywhere in the package and move them
-    # under <x-metadata>.  Force the tag to lowercase.
+    # under <metadata>.  Force the tag to lowercase.
 
-    @elements = $twigroot->children(qr/^meta$/ix);
-    foreach my $el (@elements)
-    {
+    @elements = $twigroot->descendants(qr/^meta$/ix);
+    foreach my $el (@elements) {
+        debug(2,'DEBUG: checking meta element <',$el->gi,
+              ' name="',$el->att('name'),'">');
         $el->set_gi(lc $el->gi);
         $el->move('last_child',$metadata);
     }
 
+    # Delete Calibre advertisements in dc:contributor elements
+    @elements = $twigroot->descendants('dc:contributor[string() =~ /calibre-ebook.com|calibre.kovidgoyal.net/]');
+    foreach my $el (@elements) {
+	$el->delete;
+    }
+    # Delete Aspose.Words advertisements in dc:contributor elements
+    @elements = $twigroot->descendants('dc:contributor[string() =~ /Aspose.Words/]');
+    foreach my $el (@elements) {
+	$el->delete;
+    }
+
     # Fix the <package> attributes
     $twigroot->set_att('version' => '2.0',
                        'xmlns' => 'http://www.idpf.org/2007/opf');
@@ -3812,7 +8104,8 @@ sub fix_opf20 :method
 
     # Fix the <metadata> attributes
     $metadata->set_att('xmlns:dc' => "http://purl.org/dc/elements/1.1/",
-                       'xmlns:opf' => "http://www.idpf.org/2007/opf");
+                       'xmlns:opf' => "http://www.idpf.org/2007/opf",
+                       'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance");
 
     # Fix <manifest> and <spine>
     $self->fix_manifest;
@@ -3843,8 +8136,7 @@ them moved from wherever they are.
 
 =cut
 
-sub fix_opf20_dcmetatags :method
-{
+sub fix_opf20_dcmetatags :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3854,11 +8146,9 @@ sub fix_opf20_dcmetatags :method
     my $topelement = $self->{twigroot};
     my @elements;
 
-    foreach my $dcmetatag (keys %dcelements20)
-    {
+    foreach my $dcmetatag (keys %dcelements20) {
 	@elements = $topelement->descendants(qr/^$dcmetatag$/ix);
-	foreach my $el (@elements)
-	{
+	foreach my $el (@elements) {
 	    $el->set_tag($dcelements20{lc $el->tag})
 		if($dcelements20{lc $el->tag});
 	}
@@ -3880,8 +8170,7 @@ might be very broken.
 
 =cut
 
-sub fix_packageid :method
-{
+sub fix_packageid :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3897,8 +8186,7 @@ sub fix_packageid :method
         or croak($subname,"(): metadata not found");
     my $element;
 
-    if($packageid)
-    {
+    if($packageid) {
         # Check that the ID maps to a valid identifier
 	# If not, undefine it
 	debug(2,"DEBUG: checking existing packageid '",$packageid,"'");
@@ -3914,10 +8202,8 @@ sub fix_packageid :method
 	#$element = $self->{twig}->elt_id($packageid);
 	$element = $self->{twig}->first_elt("*[\@id='$packageid']");
 
-	if($element)
-	{
-	    if(lc($element->tag) ne 'dc:identifier')
-	    {
+	if($element) {
+	    if(lc($element->tag) ne 'dc:identifier') {
 		debug(1,"DEBUG: packageid '",$packageid,
                       "' points to a non-identifier element ('",
                       $element->tag,"')");
@@ -3925,8 +8211,7 @@ sub fix_packageid :method
                       $packageid,"'");
 		undef($packageid);
 	    }
-	    elsif(!$element->text)
-	    {
+	    elsif(!$element->text) {
 		debug(1,"DEBUG: packageid '",$packageid,
                       "' points to an empty identifier.");
 		debug(1,"DEBUG: undefining existing packageid '",
@@ -3937,22 +8222,19 @@ sub fix_packageid :method
 	else { undef($packageid); };
     }
 
-    if(!$packageid)
-    {
+    if(!$packageid) {
 	# Search known IDs for a unique Package ID
 	$packageid = $self->search_knownuids;
     }
 
     # If no unique ID found so far, start searching known schemes
-    if(!$packageid)
-    {
+    if(!$packageid) {
 	$packageid = $self->search_knownuidschemes;
     }
 
     # And if we still don't have anything, we have to make one from
     # scratch using Data::UUID
-    if(!$packageid)
-    {
+    if(!$packageid) {
 	debug(1,"DEBUG: creating new UUID");
 	$element = twigelt_create_uuid();
 	$element->paste('first_child',$meta);
@@ -3976,8 +8258,7 @@ Publisher entries with no text are deleted.
 
 =cut
 
-sub fix_publisher :method
-{
+sub fix_publisher :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -3985,11 +8266,9 @@ sub fix_publisher :method
     $self->twigcheck();
 
     my @publishers = $self->twigroot->descendants(qr/^dc:publisher$/ix);
-    foreach my $pub (@publishers)
-    {
+    foreach my $pub (@publishers) {
         debug(3,"Examining publisher entry in element '",$pub->gi,"'");
-        if(!$pub->text)
-        {
+        if(!$pub->text) {
             debug(1,'Deleting empty publisher entry');
             $pub->delete;
             next;
@@ -4019,8 +8298,7 @@ Fixes problems with the OPF spine, specifically:
 
 =cut
 
-sub fix_spine :method
-{
+sub fix_spine :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -4028,32 +8306,34 @@ sub fix_spine :method
     $self->twigcheck();
 
     my $twigroot = $self->{twigroot};
+    my $manifest = $twigroot->first_descendant('manifest');
     my $spine = $twigroot->first_descendant('spine');
     my @elements;
     my $parent;
 
     @elements = $twigroot->descendants(qr/^itemref$/ix);
-    if(@elements)
-    {
+    if(@elements) {
         # If <spine> doesn't exist, create it
-        if(! $spine)
-        {
+        if(! $spine) {
             debug(1,"DEBUG: creating <spine>");
             $spine = $twigroot->insert_new_elt('last_child','spine');
         }
 
         # Make sure that the spine is a child of the twigroot,
         $parent = $spine->parent;
-        if($parent != $twigroot)
-        {
+        if($parent != $twigroot) {
             debug(1,"DEBUG: moving <spine>");
             $spine->move('last_child',$twigroot);
         }
 
-        foreach my $el (@elements)
-        {
-            if(!$el->att('idref'))
-            {
+        # If an NCX item exists in the manifest, reference it as a
+        # spine attribute
+        if($manifest->has_child('@id="ncx"')) {
+            $spine->set_att('toc' => 'ncx');
+        }
+
+        foreach my $el (@elements) {
+            if(!$el->att('idref')) {
                 # No idref means it is broken.
                 # Leave it alone, but log a warning
                 $self->add_warning(
@@ -4064,8 +8344,8 @@ sub fix_spine :method
             $el->move('last_child',$spine);
         }
     }
-    else # No elements, delete spine if it exists
-    {
+    else {
+        # No elements, delete spine if it exists
         $spine->delete if($spine);
     }
 
@@ -4073,12 +8353,165 @@ sub fix_spine :method
 }
 
 
+=head2 C<fix_subjects()>
+
+Deletes empty and duplicate subject elements and normalizes existing
+subject text against the known Library of Congress mappings.
+
+If $self->{erotic} is true, then the book will be treated as a
+work of erotic fiction and the subjects will go through preprocessing
+against the C<%sexcodes> package variable, normalizing matches and
+prepending 'FICTION / Erotica / ' (with a trailing space).
+
+This method is called as a component of C<fix_misc()>.
+
+=cut
+
+sub fix_subjects :method {
+    my ($self) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    $self->twigcheck();
+
+    my $eroticaprefix = 'FICTION / Erotica / ';
+
+    my $twigroot = $self->{twigroot};
+    my $manifest = $twigroot->first_descendant('manifest');
+    my $bisac;
+    my $newsubject;
+    my $text;
+    my $newtext;
+
+    $self->delete_subject('text' => '');
+    my @elements = $self->{twigroot}->descendants(qr/^dc:subject$/ix);
+    my %subjects_seen;
+    foreach my $el (@elements) {
+        # Matches to the list are done after stripping leading and
+        # trailing whitespace, trailing periods, and removing
+        # whitespace around '--', then converting ' - ' to '--' and
+        # decoding entities.
+        $text = trim $el->text;
+        $text =~ s|\.\z||x;
+        $text =~ s|\s* -- \s*|--|gx;
+        $text =~ s|\s* - \s*|--|gx;
+        $text =~ s|\s*—\s*|--|gx;
+        # We can't normalize spaces around slashes -- they have
+        # potentially distinct meanings
+        #$text =~ s|\s* / \s*|/|gx;
+        $text = decode_entities($text);
+
+        # Sex code matches are case-sensitive
+        if($self->{erotic}) {
+            if($sexcodes{$text}) {
+                $newtext = $eroticaprefix . $sexcodes{$text};
+                debug(1,"DEBUG: normalizing ${text} to " . $newtext);
+                if($subjects_seen{$newtext}) {
+                    $el->delete;
+                }
+                else {
+                    $el->set_text($newtext);
+                    $subjects_seen{$newtext} = 1;
+                }
+            }
+        }
+
+        # ... but all other normalizations are not.
+        $text = lc $text;
+        debug(1,"DEBUG: normalizing subject '",$text,"'");
+
+        # TODO: check for BISACCODE property, convert it to both a
+        # BISAC descriptor and a LoC subject, then remove it.
+        if($el->att('BASICCode')) {
+            $newsubject = $self->add_subject(text => $el->att('BASICCode'));
+            $el->del_att('BASICCode');
+            push(@elements,$newsubject);
+        }
+        if($bisacsubjects{$text}) {
+            debug(1,"DEBUG: found BISAC: ",$text);
+            $bisac = $bisacsubjects{$text};
+            if($subjects_seen{$bisac}) {
+                $el->delete;
+            }
+            else {
+                $el->set_text($bisac);
+                $subjects_seen{$bisac} = 1;
+            }
+        }
+
+        # TODO: check for a BISAC code or descriptor in attribute as
+        # well as text, normalize it to standard descriptor format, as
+        # well as inserting an additional matching LoC subject if
+        # present
+        if($bisactolc{$text}) {
+            $bisac = $bisactolc{$text};
+            if(! $subjects_seen{$bisac}) {
+                $self->add_subject(text => $bisac);
+            }
+        }
+        elsif($lcsubjects{$text}) {
+            $text = $lcsubjects{$text};
+            $el->set_text($text);
+        }
+
+        if($subjects_seen{$text}) {
+            $el->delete;
+        }
+        else {
+            $subjects_seen{$text} = 1;
+        }
+    }
+    return;
+}
+
+
+=head2 C<fix_type()>
+
+Normalizes <dc:type> elements against a limited list based on book types listed in Wikipedia.
+
+=cut
+
+sub fix_type :method {
+    my ($self) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    $self->twigcheck();
+
+    my $twigroot = $self->{twigroot};
+    my $manifest = $twigroot->first_descendant('manifest');
+    my $text;
+    my $type;
+
+    my @elements = $self->{twigroot}->descendants(qr/^dc:type$/ix);
+    foreach my $el (@elements) {
+        # Matches to the list are done after stripping leading and
+        # trailing whitespace, trailing periods, and removing
+        # whitespace around '--' and '/'
+        $text = lc trim $el->text;
+        $text =~ s|\.\z||x;
+        debug(2,"DEBUG: normalizing type '",$text,"'");
+
+        if($booktypes{$text}) {
+            $type = $booktypes{$text};
+            $el->set_text($type);
+        }
+        else {
+            debug(1,"WARNING: unknown book type '",$text,"'");
+        }
+    }
+    return;
+}
+
+
 =head2 C<gen_epub(%args)>
 
 Creates a .epub format e-book.  This will create (or overwrite) the
 files 'mimetype' and 'META-INF/container.xml' in the current
 directory, creating the subdirectory META-INF as needed.
 
+A NCX file will also be created if missing.
+
 =head3 Arguments
 
 This method can take two optional named arguments.
@@ -4105,10 +8538,8 @@ it will be created, or the method will croak.
 
 =cut
 
-sub gen_epub :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub gen_epub :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -4117,8 +8548,7 @@ sub gen_epub :method    ## no critic (Always unpack @_ first)
         'filename' => 1,
         'dir' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -4127,73 +8557,64 @@ sub gen_epub :method    ## no critic (Always unpack @_ first)
     my $dir = $args{dir};
     my $zip = Archive::Zip->new();
     my $member;
+    my $cwd = usedir($self->{topdir});
 
     $self->gen_epub_files();
-    if(! $self->{opffile} )
-    {
+    if(! $self->{opffile} ) {
 	$self->add_error(
 	    "Cannot create epub without an OPF (did you forget to init?)");
         debug(1,"Cannot create epub without an OPF");
 	return;
     }
-    if(! -f $self->{opffile} )
-    {
+    if(! -f $self->opfpath) {
 	$self->add_error(
 	    sprintf("OPF '%s' does not exist (did you forget to save?)",
-		    $self->{opffile})
+		    $self->opfpath)
 	    );
-        debug(1,"OPF '",$self->{opffile},"' does not exist");
+        debug(1,"OPF '",$self->opfpath,"' does not exist");
 	return;
     }
 
+    debug(3,"DEBUG: adding core metadata to zip archive");
     $member = $zip->addFile('mimetype');
     $member->desiredCompressionMethod(COMPRESSION_STORED);
 
     $member = $zip->addFile('META-INF/container.xml');
     $member->desiredCompressionLevel(9);
 
-    $member = $zip->addFile($self->{opffile});
+    $member = $zip->addFile($self->{opfsubdir} . '/' . $self->{opffile});
     $member->desiredCompressionLevel(9);
 
-    foreach my $file ($self->manifest_hrefs())
-    {
-	if(! $file)
-	{
-	    error("No items found in manifest!");
-            debug(1,"No items found in manifest!");
-	    return;
-	}
-	if(-f $file)
-	{
-	    $member = $zip->addFile($file);
+    debug(3,"DEBUG: adding manifest files to zip archive");
+    foreach my $file ($self->manifest_hrefs()) {
+        $file = uri_unescape($file);
+	if(-f $self->{opfsubdir} . '/' . $file) {
+	    $member = $zip->addFile($self->{opfsubdir} . '/' . $file);
 	    $member->desiredCompressionLevel(9);
 	}
-	else { print STDERR "WARNING: ",$file," not found, skipping.\n"; }
+	else { print STDERR "WARNING: ",$self->{opfsubdir} . '/' . $file," not found, skipping.\n"; }
     }
 
-    if(! $filename)
-    {
-	($filename) = fileparse($self->{opffile},'\.\w+$');
-	$filename .= ".epub";
+    if(! $filename) {
+	$filename = basename($self->{topdir}) . '.epub';
     }
 
-    if($dir)
-    {
-        unless(-d $dir)
-        {
+    if($dir) {
+        unless(-d $dir) {
             mkpath($dir)
                 or croak("Unable to create working directory '",$dir,"'!");
         }
         $filename = "$dir/$filename";
     }
 
-    unless ( $zip->writeToFileNamed($filename) == AZ_OK )
-    {
+    unless ( $zip->writeToFileNamed($filename) == AZ_OK ) {
 	$self->add_error(
             sprintf("Failed to create epub as '%s'",$filename));
         debug(1,"Failed to create epub as '",$filename,"'");
 	return;
     }
+
+    usedir($cwd);
     return 1;
 }
 
@@ -4204,17 +8625,39 @@ Generates the C<mimetype> and C<META-INF/container.xml> files expected
 by a .epub container, but does not actually generate the .epub file
 itself.  This will be called automatically by C<gen_epub>.
 
+The OPF will be normalized to the OPF 2.0 format.
+
+If no NCX element exists, it will also be created.
+
 =cut
 
-sub gen_epub_files :method
-{
+sub gen_epub_files :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
 
-    create_epub_mimetype();
-    create_epub_container($self->{opffile});
+    my $manifest = $self->{twigroot}->first_descendant('manifest');
+
+    $self->fix_opf20();
+    if( ! $manifest->first_child('item[@id="ncx"]') ) {
+        $self->gen_ncx();
+    }
+    $self->save();
+
+    # These two functions must happen from the top-level directory, not the OPF directory
+    if($self->{opfsubdir} ne '.') {
+        debug(3,"DEBUG: switching to ",$self->{topdir}," to generate EPUB metadata");
+        my $cwd = usedir($self->{topdir});
+        create_epub_mimetype();
+        create_epub_container($self->{opfsubdir} . '/' . $self->{opffile});
+        usedir($cwd);
+    }
+    else {
+        create_epub_mimetype();
+        create_epub_container($self->{opffile});
+    }
+
     return 1;
 }
 
@@ -4251,10 +8694,8 @@ Returns a twig containing the NCX XML, or undef on failure.
 
 =cut
 
-sub gen_ncx :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($filename) = @_;
+sub gen_ncx :method {
+    my ($self,$filename) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -4262,6 +8703,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
 
     $filename = 'toc.ncx' if(!$filename);
 
+    my $cwd = usedir($self->opfdir);
     my $twigroot = $self->{twigroot};
     my $identifier = $self->identifier;
     my $element;            # Generic element container
@@ -4277,9 +8719,9 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
     my $author;             # E-book primary author
     my @spinelist;          # List of hashrefs containing spine data
     my $manifest;           # OPF manifest element
+    my $spine;		    # OPF spine element
 
-    if($self->{spec} ne 'OPF20')
-    {
+    if($self->{spec} ne 'OPF20') {
         $self->add_error(
             $subname . "(): specification is currently set to '"
             . $self->{spec} . "' -- need 'OPF20'"
@@ -4289,8 +8731,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
         return;
     }
 
-    if(!$identifier)
-    {
+    if(!$identifier) {
         $self->add_error( $subname . "(): no unique-identifier found" );
         debug(1,"DEBUG: gen_ncx() FAILED: no unique-identifier!");
         return;
@@ -4298,8 +8739,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
 
     # Get the title
     $title = $self->title();
-    if(!$title)
-    {
+    if(!$title) {
         $self->add_error( $subname . "(): no title found" );
         debug(1,"DEBUG: gen_ncx() FAILED: no title!");
         return;
@@ -4307,8 +8747,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
 
     # Get the author
     $author = $self->primary_author();
-    if(!$author)
-    {
+    if(!$author) {
         $self->add_error( $subname . "(): no title found" );
         debug(1,"DEBUG: gen_ncx() FAILED: no title!");
         return;
@@ -4316,8 +8755,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
 
     # Get the spine list
     @spinelist = $self->spine();
-    if(!@spinelist)
-    {
+    if(!@spinelist) {
         $self->add_error( $subname . "(): no spine found" );
         debug(1,"DEBUG: gen_ncx() FAILED: no spine!");
         return;
@@ -4327,8 +8765,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
     # (This should in theory never fail, since it is also checked by
     # spine() above)
     $manifest = $twigroot->first_descendant('manifest');
-    if(!$manifest)
-    {
+    if(!$manifest) {
         $self->add_error( $subname . "(): no manifest found" );
         debug(1,"DEBUG: gen_ncx() FAILED: no manifest!");
         return;
@@ -4342,6 +8779,7 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
     # <ncx>
     $element = XML::Twig::Elt->new('ncx');
     $element->set_att('xmlns' => 'http://www.daisy.org/z3986/2005/ncx/');
+    $element->set_att('version' => '2005-1');
     $ncx->set_root($element);
     $ncxroot = $ncx->root;
 
@@ -4378,27 +8816,25 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
     # <navMap>
     $navmap = $parent->insert_new_elt('after','navMap');
 
-    foreach my $spineitem (@spinelist)
-    {
+    foreach my $spineitem (@spinelist) {
         # <navPoint>
         $navpoint = $navmap->insert_new_elt('last_child','navPoint');
-        $navpoint->set_att('id' => $$spineitem{'id'},
+        $navpoint->set_att('id' => $spineitem->{'id'},
                            'playOrder' => $navpointindex);
         $navpointindex++;
 
         # <navLabel>
         $parent = $navpoint->insert_new_elt('last_child','navLabel');
         $element = $parent->insert_new_elt('last_child','text');
-        $element->set_text($$spineitem{'id'});
+        $element->set_text($spineitem->{'id'});
 
         # <content>
         $element = $navpoint->insert_new_elt('last_child','content');
-        $element->set_att('src' => $$spineitem{'href'});
+        $element->set_att('src' => $spineitem->{'href'});
     }
 
     # Backup existing file
-    if(-e $filename)
-    {
+    if(-e $filename) {
         rename($filename,"$filename.backup")
             or croak($subname,"(): could not backup ",$filename,"!");
     }
@@ -4430,6 +8866,11 @@ sub gen_ncx :method    ## no critic (Always unpack @_ first)
     # Move the NCX item to the top of the manifest
     $ncxitem->move('first_child',$manifest);
 
+    # Ensure that the spine references the NCX
+    $spine = $twigroot->first_descendant('spine');
+    $spine->set_att('toc' => 'ncx');
+
+    usedir($cwd);
     return $ncx;
 }
 
@@ -4441,8 +8882,7 @@ filename.backup.
 
 =cut
 
-sub save :method
-{
+sub save :method {
     my $self = shift;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
@@ -4452,30 +8892,33 @@ sub save :method
         if(!$self->{opffile});
 
     my $fh_opf;
+    my $cwd = usedir($self->opfdir);
     my $filename = $self->{opffile};
 
     # Backup existing file
-    if(-e $filename)
-    {
+    if(-e $filename) {
         rename($filename,"$filename.backup")
             or croak($subname,"(): could not backup ",$filename,"!");
     }
 
+    # Update the last-modified timestamp as the very last thing before
+    # saving if the output changed at all
+    if($self->{twig}->sprint ne $self->{twig_unmodified}->sprint) {
+        $self->set_timestamp();
+    }
 
-    # Twig handles utf8 on its own.  If you open this file with
-    # binmode :utf8, it will double-convert.
-    if(!open($fh_opf,">",$self->{opffile}))
-    {
+    if(!open($fh_opf,">:encoding(UTF-8)",$self->{opffile})) {
 	add_error(sprintf("Could not open '%s' to save to!",$self->{opffile}));
 	return;
     }
     $self->{twig}->print(\*$fh_opf);
 
-    if(!close($fh_opf))
-    {
+    if(!close($fh_opf)) {
 	add_error(sprintf("Failure while closing '%s'!",$self->{opffile}));
 	return;
     }
+
+    usedir($cwd);
     return 1;
 }
 
@@ -4494,8 +8937,7 @@ Mobipocket's software when placed directly under <metadata>
 
 =cut
 
-sub set_adult :method
-{
+sub set_adult :method {
     my $self = shift;
     my $adult = shift;
     my $subname = ( caller(0) )[3];
@@ -4509,22 +8951,18 @@ sub set_adult :method
     my $element;
     my @elements;
 
-    if($adult)
-    {
+    if($adult) {
         $element = $self->{twigroot}->first_descendant(qr/^adult$/ix);
-        unless($element)
-        {
+        unless($element) {
             $self->fix_metastructure_oeb12();
             $xmeta = $self->{twigroot}->first_descendant('x-metadata');
             $element = $xmeta->insert_new_elt('last_child','Adult');
         }
         $element->set_text('yes');
     }
-    else
-    {
+    else {
         @elements = $self->{twigroot}->descendants(qr/^adult$/ix);
-        foreach my $el (@elements)
-        {
+        foreach my $el (@elements) {
             debug(2,"DEBUG: deleting <Adult> flag");
             $el->delete;
         }
@@ -4533,6 +8971,153 @@ sub set_adult :method
 }
 
 
+=head2 C<set_cover(%args)>
+
+Sets a cover image
+
+In OPF 2.0, this is done by setting both a <meta name="cover"> element
+and a guide <reference type="other.ms-coverimage-standard"> element
+(though some readers will also extract the first image found in the
+HTML of the <reference type="cover"> element, which this method will
+not handle).
+
+In OEB 1.2, this is done by setting the <EmbeddedCover> tag.
+
+If the filename is not currently listed as an item in the manifest, it
+is added.
+
+=head3 Arguments
+
+=over
+
+=item C<href>
+
+The filename of the image file to use.  This is mandatory.
+
+=item C<id>
+
+The id attribute to assign to its item element
+
+=item C<spec>
+
+The specification to use, either OEB12 or OPF20.  If this is left
+undefined, the current spec state will be checked, and if that is
+undefined, it will default to OPF20.
+
+=back
+
+=cut
+
+sub set_cover :method {
+    my ($self,%args) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+    my %valid_args = (
+        'href' => 1,
+        'id' => 1,
+        'spec' => 1,
+        );
+    foreach my $arg (keys %args) {
+        croak($subname,"(): invalid argument '",$arg,"'")
+            if(!$valid_args{$arg});
+    }
+
+    my $href = $args{href};
+    my $newid = $args{id};
+    my $spec = $args{spec};
+    my $id;
+    my $mimetype;
+    my $manifest;
+    my $guide;
+    my $dcmeta;
+    my $element;
+
+    if(! $href) {
+        $self->add_error($subname,"(): no href specified");
+        return;
+    }
+    $mimetype = mimetype($href);
+    if($mimetype !~ m#^image/#ix) {
+        $self->add_warning(
+            $subname,"(): ",$href,
+            " does not appear to be an image (detected: ",$mimetype,")"
+           );
+    }
+
+    if(! $spec) {
+        $spec = $self->spec || 'OPF20';
+    }
+
+    # Ensure that there is a matching manifest item
+    $manifest = $self->{twigroot}->first_child('manifest');
+    $element = $manifest->first_child('item[@href="' . $href . '"]');
+    if($element) {
+        if($newid) {
+            $element->set_id($newid);
+        }
+    }
+    else {
+        $element = $manifest->insert_new_elt('first_child','item');
+        if ($newid) {
+            $element->set_id($newid);
+        }
+        elsif ($self->{twig}->first_elt('*[@id="coverimage"]')) {
+            $element->set_id(basename($href));
+        }
+        else {
+            $element->set_id('coverimage');
+        }
+        $element->set_att('href',$href);
+        $element->set_att('media-type',$mimetype);
+    }
+    $id = $element->id;
+
+    if ($spec eq 'OPF20') {
+        $self->fix_metastructure_basic;
+        $self->fix_guide;
+        $guide = $self->{twigroot}->first_child('guide');
+        $element = $guide->first_child('reference[@type="other.ms-coverimage-standard"]');
+        if ($element) {
+            $element->set_att('href',$href);
+            $element->set_att('title','Cover');
+        }
+        else {
+            $element = $guide->insert_new_elt('last_child','reference');
+            $element->set_att('href',$href);
+            $element->set_att('title','Cover');
+            $element->set_att('type','other.ms-coverimage-standard');
+        }
+        $self->set_meta('name' => 'cover',
+                        'content' => $href);
+
+        # Now that the OPF 2.0 elements are set, we can delete the
+        # OEB12 EmbeddedCover element
+        $element = $self->{twigroot}->first_descendant('EmbeddedCover');
+        if ($element) {
+            debug(1,"deleting cover");
+            $element->delete;
+        }
+    }
+    elsif ($spec eq 'OEB12') {
+        $self->fix_metastructure_oeb12;
+        $dcmeta = $self->{twigroot}->first_child('dc-metadata');
+        $element = $dcmeta->first_child('EmbeddedCover');
+        if ($element) {
+            $element->set_text($href);
+        }
+        else {
+            $element = $dcmeta->insert_new_elt('last_child','EmbeddedCover');
+            $element->set_text($href);
+        }
+    }
+    else {
+        self->add_error($subname,"(): unknown specification type: '",$spec,"'");
+    }
+    return;
+}
+
+
 =head2 C<set_date(%args)>
 
 Sets the date metadata for a given event.  If more than one dc:date or
@@ -4559,8 +9144,9 @@ not specified, the method sets an error and returns undef.
 
 =item C<event>
 
-This specifies the event attribute for the date.  If not specified,
-the method sets an error and returns undef.
+This optionally specifies the event attribute for the date.  This
+attribute is not valid in OPF 3.0 (which only allows publication date
+in this element) and should no longer be used.
 
 =item C<id> (optional)
 
@@ -4572,10 +9158,8 @@ other location and assigned to the element.
 
 =cut
 
-sub set_date :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_date :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -4583,26 +9167,19 @@ sub set_date :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'event' => 1,
         'id' => 1,
-        );
-    foreach my $arg (keys %args)
-    {
+       );
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
-            if(!$valid_args{$arg});
+          if (!$valid_args{$arg});
     }
 
     my $text = $args{text};
     my $event = $args{event};
     my $newid = $args{id};
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
-    unless($event)
-    {
-        $self->add_error($subname,"(): no event specified");
-        return;
-    }
 
     my $meta = $self->{twigroot}->first_child('metadata');
     my $dcmeta = $meta->first_child('dc-metadata');
@@ -4611,35 +9188,43 @@ sub set_date :method    ## no critic (Always unpack @_ first)
 
     $self->fix_metastructure_basic();
 
-    my $element = $self->{twigroot}->first_descendant(
-        "dc:date[\@opf:event=~/$event/ix or \@event=~/$event/ix]");
-    $element = $self->{twigroot}->first_descendant(
-        "dc:Date[\@opf:event=~/$event/ix or \@event=~/$event/ix]")
-        unless($element);
+    my $element;
+    if ($event) {
+        $element = $self->{twigroot}->first_descendant(
+            "dc:date[\@opf:event=~/$event/ix or \@event=~/$event/ix]");
+        $element = $self->{twigroot}->first_descendant(
+            "dc:Date[\@opf:event=~/$event/ix or \@event=~/$event/ix]")
+          unless($element);
+    }
+    else {
+        $element = $self->{twigroot}->first_descendant("dc:date");
+        $element = $self->{twigroot}->first_descendant("dc:Date")
+          unless($element);
+    }
 
-    if($element)
-    {
+    if ($element) {
         $element->set_text($text);
     }
-    elsif($dcmeta)
-    {
+    elsif ($dcmeta) {
         $element = $dcmeta->insert_new_elt('last_child','dc:Date');
-        $element->set_att('event',$event);
         $element->set_text($text);
+        if ($event) {
+            $element->set_att('event',$event);
+        }
     }
-    else
-    {
+    else {
         $element = $meta->insert_new_elt('last_child','dc:date');
-        $element->set_att('opf:event',$event);
         $element->set_text($text);
+        if ($event) {
+            $element->set_att('opf:event',$event);
+        }
     }
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if ($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
-            );
+           );
         $idelem->del_att('id');
     }
     $element->set_att('id' => $newid) if($newid);
@@ -4684,26 +9269,22 @@ other location and assigned to the element.
 
 =cut
 
-sub set_description :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_description :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     my %valid_args = (
         'text' => 1,
         'id' => 1,
-        );
-    foreach my $arg (keys %args)
-    {
+       );
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
-            if(!$valid_args{$arg});
+          if (!$valid_args{$arg});
     }
 
     my $text = $args{text};
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
@@ -4722,6 +9303,39 @@ sub set_description :method    ## no critic (Always unpack @_ first)
 }
 
 
+=head2 C<set_erotic($bool)>
+
+If C<$bool> is true, C<$self->{erotic}> is set to 1, otherwise this is
+set to 0.
+
+This will enable or disable special handling for erotic books, most
+notably in subject normalization.
+
+This is not related in any way to C<set_adult> which is a
+Mobipocket-specific flag.
+
+Returns 1 if no argument is given, 0 otherwise.
+
+=cut
+
+sub set_erotic :method {
+    my ($self,$flag) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+
+    return 1 unless(defined $flag);
+
+    if($flag) {
+        $self->{erotic} = 1;
+    }
+    else {
+        $self->{erotic} = 0;
+    }
+    return 0;
+}
+
+
 =head2 C<set_language(%args)>
 
 Sets the text and optionally the ID of the first dc:language element
@@ -4758,26 +9372,22 @@ other location and assigned to the element.
 
 =cut
 
-sub set_language :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_language :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
     my %valid_args = (
         'text' => 1,
         'id' => 1,
-        );
-    foreach my $arg (keys %args)
-    {
+       );
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
-            if(!$valid_args{$arg});
+          if (!$valid_args{$arg});
     }
 
     my $text = lc($args{text});
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
@@ -4795,6 +9405,183 @@ sub set_language :method    ## no critic (Always unpack @_ first)
 }
 
 
+=head2 set_meta(%args)
+
+Sets a <meta> element in the <metadata> element area.
+
+=head3 Arguments
+
+=over
+
+=item C<name>
+
+The name attribute to use when finding or creating OPF 2.0 <meta>
+elements.  Either this or the property attribute (below) must be
+specified, but specifying both is an error.
+
+=item C<content>
+
+The value of the content attribute to set on OPF 2.0 elements.  If
+this value is empty or undefined, but C<name> is provided and matches
+an existing element, that element will be deleted.
+
+=item C<property>
+
+The property attribute to use when finding or creating OPF 3.0 <meta>
+elements.  Either this or C<name> (above) must be specified, but
+specifying both is an error.
+
+=item C<refines>
+
+The refines attribute to use when finding or creating OPF 3.0 <meta>
+elements.
+
+=item C<scheme>
+
+The scheme attribute to use when creating or updating OPF 3.0 <meta>
+elements.
+
+=item C<text>
+
+The text set on OPF 3.0 <meta> elements.  If this value is empty or
+undefined, but C<property> is provided and the combination of
+C<property> and C<refines> matches an existing element, that element
+will be deleted.
+
+=item C<lang>
+
+The xml:lang attribute to set.  This is valid on both OPF 2 and OPF 3
+<meta> elements.
+
+=back
+
+=cut
+
+sub set_meta :method {
+    my ($self, %args) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(3,"DEBUG[",$subname,"]");
+    my %valid_args = (
+        'name'     => 1,
+        'content'  => 1,
+        'property' => 1,
+        'refines'  => 1,
+        'scheme'   => 1,
+        'text'     => 1,
+        'lang'     => 1,
+       );
+    foreach my $arg (keys %args) {
+        croak($subname,"(): invalid argument '",$arg,"'")
+          if (!$valid_args{$arg});
+    }
+
+    my $name = $args{name};
+    my $content = $args{content};
+    my $property = $args{property};
+    my $refines = $args{refines};
+    my $scheme = $args{scheme};
+    my $text = $args{text};
+    my $lang = $args{lang};
+    my $standard;
+
+    if ($name) {
+        if ($property) {
+            $self->add_error($subname,"(): both name (OPF2) and property (OPF3) attributes specified for a meta tag");
+            return;
+        }
+        $standard = 'OPF2';
+    }
+    else {
+        if ($property) {
+            $standard = 'OPF3';
+        }
+        else {
+            $self->add_error($subname,"(): neither name (OPF2) nor property (OPF3) attributes specified for a meta tag");
+            return;
+        }
+    }
+
+    $self->fix_metastructure_basic();
+    my $metadata = $self->{twigroot}->first_child('metadata');
+    my $element;
+
+    if ($standard eq 'OPF2') {
+        $element = $metadata->first_descendant('meta[@name="' . $name . '"]');
+
+        if ($element) {
+            if (! $content) {
+                debug(2,"DEBUG: deleting <meta name='",$name,"'>");
+                $element->delete;
+            }
+            else {
+                debug(2,"DEBUG: updating <meta name='",$name,"'>");
+                $element->set_att('content',$content);
+                if ($lang) {
+                    $element->set_att('xml:lang',$lang);
+                }
+            }
+        }
+        else {
+            if ($content) {
+                debug(2,"DEBUG: creating <meta name='",$name,"'>");
+                $element = $metadata->insert_new_elt('last_child','meta');
+                $element->set_att('name',$name);
+                $element->set_att('content',$content);
+            }
+        }
+    }
+    elsif ($standard eq 'OPF3') {
+        if ($refines) {
+            $element = $metadata->first_descendant(
+                'meta[@property="' . $property . '" and @refines="' . $refines . '"]');
+        }
+        else {
+            $element = $metadata->first_descendant(
+                'meta[@property="' . $property . '"]');
+        }
+        if ($element) {
+            if (! $text) {
+                debug(2,"DEBUG: deleting meta property='",$property,"'>");
+                $element->delete;
+            }
+            else {
+                debug(2,"DEBUG: updating <meta property='",$property,"'>");
+                $element->set_text($text);
+            }
+            if ($scheme) {
+                $element->set_att('scheme',$scheme);
+            }
+            if ($lang) {
+                $element->set_att('xml:lang',$lang);
+            }
+        }
+        else {
+            if ($text) {
+                debug(2,"DEBUG: creating <meta property='",$property,"'>");
+                $element = $metadata->insert_new_elt('last_child','meta');
+                $element->set_att('property',$property);
+                if ($refines) {
+                    $element->set_att('refines',$refines);
+                }
+                if ($scheme) {
+                    $element->set_att('scheme',$scheme);
+                }
+                if ($lang) {
+                    $element->set_att('xml:lang',$lang);
+                }
+                $element->set_text($text);
+            }
+        }
+    }
+    else {
+        croak($subname,"(): unknown standard '${standard}'! (This should be impossible!)");
+    }
+
+    return;
+}
+
+
 =head2 set_metadata(%args)
 
 Sets the text and optionally the ID of the first specified element
@@ -4868,10 +9655,8 @@ This specifies the scheme attribute to set on the element.
 
 =cut
 
-sub set_metadata :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_metadata :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(3,"DEBUG[",$subname,"]");
@@ -4884,22 +9669,19 @@ sub set_metadata :method    ## no critic (Always unpack @_ first)
         'role' => 1,
         'scheme' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
 
     my $gi = $args{gi};
-    unless($gi)
-    {
+    unless($gi) {
         $self->add_error($subname,"(): no gi specified");
         return;
     }
 
     my $text = $args{text};
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
@@ -4920,16 +9702,14 @@ sub set_metadata :method    ## no critic (Always unpack @_ first)
     $meta = $self->{twigroot}->first_child('metadata');
     $dcmeta = $meta->first_child('dc-metadata');
     $parent = $parent || $dcmeta || $meta;
-    if($parent->gi eq 'metadata')
-    {
+    if($parent->gi eq 'metadata') {
         %dcatts = (
             'file-as' => 'opf:file-as',
             'role' => 'opf:role',
             'scheme' => 'opf:scheme',
             );
     }
-    else
-    {
+    else {
         %dcatts = (
             'file-as' => 'file-as',
             'role' => 'role',
@@ -4938,51 +9718,42 @@ sub set_metadata :method    ## no critic (Always unpack @_ first)
     }
 
 
-    if($element)
-    {
+    if($element) {
         debug(2,"DEBUG: updating '",$gi,"'");
-        if($element->att('opf:file-as') && $args{fileas})
-        {
+        if($element->att('opf:file-as') && $args{fileas}) {
             debug(3,"DEBUG:   setting opf:file-as '",$args{fileas},"'");
             $element->set_att('opf:file-as',$args{fileas});
         }
-        elsif($args{fileas})
-        {
+        elsif($args{fileas}) {
             debug(3,"DEBUG:   setting file-as '",$args{fileas},"'");
             $element->set_att('file-as',$args{fileas});
         }
-        if($element->att('opf:role') && $args{role})
-        {
+        if($element->att('opf:role') && $args{role}) {
             debug(3,"DEBUG:   setting opf:role '",$args{role},"'");
             $element->set_att('opf:role',$args{role});
         }
-        elsif($args{role})
-        {
+        elsif($args{role}) {
             debug(3,"DEBUG:   setting role '",$args{role},"'");
             $element->set_att('role',$args{role});
         }
-        if($element->att('opf:scheme') && $args{scheme})
-        {
+        if($element->att('opf:scheme') && $args{scheme}) {
             debug(3,"DEBUG:   setting opf:scheme '",$args{scheme},"'");
             $element->set_att('opf:scheme',$args{scheme});
         }
-        elsif($args{scheme})
-        {
+        elsif($args{scheme}) {
             debug(3,"DEBUG:   setting scheme '",$args{scheme},"'");
             $element->set_att('scheme',$args{scheme});
         }
         debug(3,"DEBUG:   setting text");
         $element->set_text($text);
 
-        unless($element->parent->gi eq $parent->gi)
-        {
+        unless($element->parent->gi eq $parent->gi) {
             debug(2,"DEBUG: moving <",$element->gi,"> under <",
                   $parent->gi,">");
             $element->move('last_child',$parent);
         }
     }
-    else
-    {
+    else {
         debug(2,"DEBUG: creating '",$gi,"' under <",$parent->gi,">");
         $element = $parent->insert_new_elt('last_child',$gi);
         $element->set_att($dcatts{'file-as'},$args{fileas})
@@ -4994,8 +9765,7 @@ sub set_metadata :method    ## no critic (Always unpack @_ first)
         $element->set_text($text);
     }
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' to a '",$element->gi,"'!"
@@ -5016,16 +9786,13 @@ filename was specified.
 
 =cut
 
-sub set_opffile :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($filename) = @_;
+sub set_opffile :method {
+    my ($self,$filename) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
 
-    unless($filename)
-    {
+    unless($filename) {
         debug(1,$subname,"(): no filename specified!");
         $self->add_warning($subname,"(): no filename specified!");
         return;
@@ -5064,10 +9831,8 @@ defaults to 'USD' (US Dollars)
 
 =cut
 
-sub set_retailprice :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my %args = @_;
+sub set_retailprice :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5078,13 +9843,11 @@ sub set_retailprice :method    ## no critic (Always unpack @_ first)
         'currency' => 1,
         );
 
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
-    unless(defined $args{text})
-    {
+    unless(defined $args{text}) {
         $self->add_error($subname,"(): text not defined");
         return;
     }
@@ -5093,11 +9856,9 @@ sub set_retailprice :method    ## no critic (Always unpack @_ first)
     my $element;
     my @elements;
 
-    if($args{text})
-    {
+    if($args{text}) {
         $element = $self->{twigroot}->first_descendant(qr/^ SRP $/ix);
-        unless($element)
-        {
+        unless($element) {
             $self->fix_metastructure_oeb12();
             $xmeta = $self->{twigroot}->first_descendant('x-metadata');
             $element = $xmeta->insert_new_elt('last_child','SRP');
@@ -5105,11 +9866,9 @@ sub set_retailprice :method    ## no critic (Always unpack @_ first)
         $element->set_text($args{text});
         $element->set_att('Currency',$args{currency}) if($args{currency});
     }
-    else
-    {
+    else {
         @elements = $self->{twigroot}->descendants(qr/^ SRP $/ix);
-        foreach my $el (@elements)
-        {
+        foreach my $el (@elements) {
             debug(2,"DEBUG: deleting <SRP>");
             $el->delete;
         }
@@ -5180,10 +9939,8 @@ found.
 
 =cut
 
-sub set_primary_author :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_primary_author :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5192,8 +9949,7 @@ sub set_primary_author :method    ## no critic (Always unpack @_ first)
         'fileas' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -5212,23 +9968,19 @@ sub set_primary_author :method    ## no critic (Always unpack @_ first)
     $element = $twigroot->first_descendant(\&twigelt_is_author);
     $element = $twigroot->first_descendant(qr/dc:creator/ix) if(!$element);
 
-    unless($element)
-    {
-        unless($newauthor)
-        {
+    unless($element) {
+        unless($newauthor) {
             add_error(
                 $subname,
                 "(): cannot create a new author element when the author is not specified");
             return;
         }
-        if($dcmeta)
-        {
+        if($dcmeta) {
             $element = $dcmeta->insert_new_elt('last_child','dc:Creator');
             $element->set_att('role' => 'aut');
             $element->set_att('file-as' => $newfileas) if($newfileas);
         }
-        else
-        {
+        else {
             $element = $meta->insert_new_elt('last_child','dc:creator');
             $element->set_att('opf:role' => 'aut');
             $element->set_att('opf:file-as' => $newfileas) if($newfileas);
@@ -5236,8 +9988,7 @@ sub set_primary_author :method    ## no critic (Always unpack @_ first)
     } # unless($element)
     $element->set_text($newauthor);
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -5286,10 +10037,8 @@ other location and assigned to the element.
 
 =cut
 
-sub set_publisher :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_publisher :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5297,8 +10046,7 @@ sub set_publisher :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -5315,17 +10063,14 @@ sub set_publisher :method    ## no critic (Always unpack @_ first)
     my $meta = $self->{twigroot}->first_child('metadata');
     my $dcmeta = $meta->first_child('dc-metadata');
 
-    if(!$element && $dcmeta)
-    {
+    if(!$element && $dcmeta) {
         $element = $dcmeta->insert_new_elt('last_child','dc:Publisher');
     }
-    elsif(!$element)
-    {
+    elsif(!$element) {
         $element = $meta->insert_new_elt('last_child','dc:publisher');
     }
     $element->set_text($publisher);
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -5373,10 +10118,8 @@ other location and assigned to the element.
 
 =cut
 
-sub set_review :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_review :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5384,15 +10127,13 @@ sub set_review :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
 
     my $text = $args{text};
-    unless($text)
-    {
+    unless($text) {
         $self->add_error($subname,"(): no text specified");
         return;
     }
@@ -5439,10 +10180,8 @@ already in use, a warning is logged but the method continues anyway.
 
 =cut
 
-sub set_rights :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_rights :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5450,8 +10189,7 @@ sub set_rights :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -5471,8 +10209,7 @@ sub set_rights :method    ## no critic (Always unpack @_ first)
     $element->set_text($rights);
     $element->set_gi('dc:Rights') if($element->gi eq 'dc:Copyrights');
     $element->set_gi('dc:rights') if($element->gi eq 'dc:copyrights');
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -5494,16 +10231,13 @@ unknown specification was set.
 
 =cut
 
-sub set_spec :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my ($spec) = @_;
+sub set_spec :method {
+    my ($self,$spec) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
 
-    unless($validspecs{$spec})
-    {
+    unless($validspecs{$spec}) {
         $self->add_error($subname,"(): invalid specification '",$spec,"'");
         return;
     }
@@ -5512,6 +10246,40 @@ sub set_spec :method    ## no critic (Always unpack @_ first)
 }
 
 
+=head2 C<set_timestamp()>
+
+Sets the <meta property="dcterms:modified"> element to the current
+timestamp and removes duplicate or nonstandard timestamps.
+
+=cut
+
+sub set_timestamp :method {
+    my ($self) = @_;
+    my $subname = ( caller(0) )[3];
+    croak($subname . "() called as a procedure") unless(ref $self);
+    debug(2,"DEBUG[",$subname,"]");
+
+    my @timestamps;
+    my $twigroot = $self->{twigroot};
+    my $time = Date::Manip::Date->new();
+
+    @timestamps = $twigroot->descendants(
+        'meta[@property="dcterms:modified" or @name="calibre:timestamp"]');
+    foreach my $timestamp (@timestamps) {
+        $timestamp->delete;
+    }
+    @timestamps = $twigroot->descendants(
+        'dc:date[@opf:event="modification" or @event="modification"]');
+    foreach my $timestamp (@timestamps) {
+        $timestamp->delete;
+    }
+    $time->parse('now');
+    $self->set_meta('property' => 'dcterms:modified',
+                    'text' => $time->printf('%O') );
+    return;
+}
+
+
 =head2 C<set_title(%args)>
 
 Sets the text or id of the first dc:title element found
@@ -5544,10 +10312,8 @@ already in use, a warning is logged but the method continues anyway.
 
 =cut
 
-sub set_title :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_title :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5556,8 +10322,7 @@ sub set_title :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -5571,10 +10336,8 @@ sub set_title :method    ## no critic (Always unpack @_ first)
     my $meta = $self->{twigroot}->first_child('metadata');
     my $dcmeta = $meta->first_child('dc-metadata');
     my $parent = $dcmeta || $meta;
-    unless($element)
-    {
-        unless($title)
-        {
+    unless($element) {
+        unless($title) {
             add_error($subname,
                       "(): no title specified, but no existing title found");
             return;
@@ -5583,8 +10346,7 @@ sub set_title :method    ## no critic (Always unpack @_ first)
     }
     $element->set_text($title) if($title);
 
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -5633,10 +10395,8 @@ other location and assigned to the element.
 
 =cut
 
-sub set_type :method    ## no critic (Always unpack @_ first)
-{
-    my $self = shift;
-    my (%args) = @_;
+sub set_type :method {
+    my ($self,%args) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname . "() called as a procedure") unless(ref $self);
     debug(2,"DEBUG[",$subname,"]");
@@ -5644,8 +10404,7 @@ sub set_type :method    ## no critic (Always unpack @_ first)
         'text' => 1,
         'id' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -5662,17 +10421,14 @@ sub set_type :method    ## no critic (Always unpack @_ first)
     my $meta = $self->{twigroot}->first_child('metadata');
     my $dcmeta = $meta->first_child('dc-metadata');
 
-    if(!$element && $dcmeta)
-    {
+    if(!$element && $dcmeta) {
         $element = $dcmeta->insert_new_elt('last_child','dc:Type');
     }
-    elsif(!$element)
-    {
+    elsif(!$element) {
         $element = $meta->insert_new_elt('last_child','dc:type');
     }
     $element->set_text($text);
-    if($idelem && $idelem->cmp($element) )
-    {
+    if($idelem && $idelem->cmp($element) ) {
         $self->add_warning(
             $subname,"(): reassigning id '",$newid,
             "' from a '",$idelem->gi,"' element!"
@@ -5694,6 +10450,69 @@ All procedures are exportable, but none are exported by default.  All
 procedures can be exported by using the ":all" tag.
 
 
+=head2 C<_lc>
+
+Wrapper for CORE::lc to get around the fact that builtins can't be
+used in dispatch tables prior to Perl 5.16.
+
+WARNING: this procedure may disappear once Perl 5.16 is standard on
+all systems in common use!  For that reason, this is not exportable.
+
+=cut
+
+sub _lc {
+    my ($string) = @_;
+    return lc $string;
+}
+
+
+=head2 C<_uc>
+
+Wrapper for CORE::uc to get around the fact that builtins can't be
+used in dispatch tables prior to Perl 5.16.
+
+WARNING: this procedure may disappear once Perl 5.16 is standard on
+all systems in common use!  For that reason, this is not exportable.
+
+=cut
+
+sub _uc {
+    my ($string) = @_;
+    return uc $string;
+}
+
+
+=head2 C<capitalize($string)>
+
+Capitalizes the first letter of each word in $string.
+
+Returns the corrected string.
+
+=cut
+
+sub capitalize {
+    my ($string) = @_;
+    $string =~ s/(?<=\w)(.)/\l$1/gx;
+    return $string;
+}
+
+
+=head2 C<clean_filename($string)>
+
+Takes an input string and cleans out any characters that would not be
+valid in a filename.
+
+Returns the cleaned string.
+
+=cut
+
+sub clean_filename {
+    my ($string) = @_;
+    $string =~ s/[^A-Za-z0-9_ \-\.]//g;
+    return $string;
+}
+
+
 =head2 C<create_epub_container($opffile)>
 
 Creates the XML file META-INF/container.xml pointing to the
@@ -5727,8 +10546,7 @@ otherwise
 
 =cut
 
-sub create_epub_container
-{
+sub create_epub_container {
     my ($opffile) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -5741,10 +10559,8 @@ sub create_epub_container
 
     if($opffile eq '') { return; }
 
-    if(-e 'META-INF')
-    {
-	if(! -d 'META-INF')
-	{
+    if(-e 'META-INF') {
+	if(! -d 'META-INF') {
 	    unlink('META-INF') or return;
 	    mkdir('META-INF') or return;
 	}
@@ -5769,15 +10585,12 @@ sub create_epub_container
 
 
     # Backup existing file
-    if(-e 'META-INF/container.xml')
-    {
+    if(-e 'META-INF/container.xml') {
         rename('META-INF/container.xml','META-INF/container.xml.backup')
             or croak($subname,"(): could not backup container.xml!");
     }
 
-    # Twig handles utf-8 on its own.  Setting binmode :utf8 here will
-    # cause a double-conversion.
-    open($fh_container,'>','META-INF/container.xml')
+    open($fh_container,'>:encoding(UTF-8)','META-INF/container.xml')
         or croak($subname,"(): could not write to 'META-INF/container.xml'\n");
     $twig->print(\*$fh_container);
     close($fh_container)
@@ -5797,8 +10610,7 @@ Returns the mimetype string if successful, undef otherwise.
 
 =cut
 
-sub create_epub_mimetype
-{
+sub create_epub_mimetype {
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
 
@@ -5823,8 +10635,7 @@ Returns true or dies.
 
 =cut
 
-sub debug
-{
+sub debug {
     my ($level,@message) = @_;
     my $subname = ( caller(0) )[3];
     croak($subname,"(): no debugging level specified") unless($level);
@@ -5847,8 +10658,7 @@ characters.
 
 =cut
 
-sub excerpt_line
-{
+sub excerpt_line {
     my @parts = @_;
     my $subname = ( caller(0) )[3];
     my $text = join('',@parts);
@@ -5877,8 +10687,7 @@ no pattern was specified.
 
 =cut
 
-sub find_in_path
-{
+sub find_in_path {
     my ($pattern,@extradirs) = @_;
     return unless($pattern);
     my $subname = ( caller(0) )[3];
@@ -5890,8 +10699,7 @@ sub find_in_path
     my @filelist;
     my $envsep = ':';
     my $filesep = '/';
-    if($OSNAME eq 'MSWin32')
-    {
+    if($OSNAME eq 'MSWin32') {
         $envsep = ';';
         $filesep = "\\";
     }
@@ -5901,12 +10709,9 @@ sub find_in_path
 
     @dirs = split(/$envsep/,$ENV{PATH});
     unshift(@dirs,@extradirs) if(@extradirs);
-    foreach my $dir (@dirs)
-    {
-        if(-d $dir)
-        {
-            if(opendir($fh_dir,$dir))
-            {
+    foreach my $dir (@dirs) {
+        if(-d $dir) {
+            if(opendir($fh_dir,$dir)) {
                 @filelist = grep { /$regexp/ } readdir($fh_dir);
                 @filelist = grep { -f "$dir/$_" } @filelist;
                 closedir($fh_dir);
@@ -5942,8 +10747,7 @@ This also does not distinguish between local files and remote links.
 
 =cut
 
-sub find_links
-{
+sub find_links {
     my ($filename) = @_;
     return unless(-f $filename);
     my $subname = ( caller(0) )[3];
@@ -5953,18 +10757,44 @@ sub find_links
     my %linkhash;
     my @links;
 
+    my $subdir = dirname($filename);
+    if($subdir eq '.') {
+        $subdir = '';
+    }
+    else {
+        $subdir = $subdir . '/';
+    }
+
     open($fh,'<:raw',$filename)
         or croak($subname,"(): unable to open '",$filename,"'\n");
 
-    while(<$fh>)
-    {
+    while(<$fh>) {
         @links = /(?:href|src) \s* = \s* "
                   ([^">]+)/gix;
-        foreach my $link (@links)
-        {
+        foreach my $link (@links) {
+            # Perform URI decoding
+            $link = uri_unescape($link);
+
             # Strip off any named anchors
             $link =~ s/#.*$//;
-            next unless($link);
+            next unless $link;
+
+            # Skip links that begin with backwards directory traversal
+            next if $link =~ m#^\.\./.*$#;
+
+            # Strip javascript: hrefs
+            next if $link =~ m#javascript:#;
+
+            # Baen HTML sources in particular may contain javascript
+            # href generators
+            next if $link =~ /\+ /;
+
+            # If the link is not a URI and we are in a subdirectory
+            # relative to the OPF, ensure that subdirectory is placed
+            # as a prefix.
+            if($subdir and $link !~ m#^ \w+://#ix) {
+                $link = $subdir . $link;
+            }
             debug(2, "DEBUG: found link '",$link,"'");
             $linkhash{$link}++;
         }
@@ -5985,22 +10815,18 @@ Returns the filename of the OPF file, or undef if nothing was found.
 
 =cut
 
-sub find_opffile
-{
+sub find_opffile {
     my $subname = ( caller(0) )[3];
     my $opffile = get_container_rootfile();
 
-    if(!$opffile)
-    {
+    if(!$opffile) {
 	my @candidates = glob("*.opf");
-        if(scalar(@candidates) > 1)
-        {
+        if(scalar(@candidates) > 1) {
             debug(1,"DEBUG: Multiple OPF files found, but no container",
                   " to specify which one to choose!");
             return;
         }
-        if(scalar(@candidates) < 1)
-        {
+        if(scalar(@candidates) < 1) {
             debug(1,"DEBUG: No OPF files found!");
             return;
         }
@@ -6043,8 +10869,7 @@ A date string in a format recognizable by Date::Manip
 
 =cut
 
-sub fix_datestring
-{
+sub fix_datestring {
     my ($datestring) = @_;
     return unless($datestring);
     my $subname = ( caller(0) )[3];
@@ -6057,13 +10882,11 @@ sub fix_datestring
     $_ = $datestring;
 
     debug(3,"DEBUG: checking M(M)/D(D)/YYYY");
-    if(( ($month,$day,$year) = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/x ) == 3)
-    {
+    if(( ($month,$day,$year) = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/x ) == 3) {
 	# We have a XX/XX/XXXX datestring
 	debug(3,"DEBUG: found '",$month,"/",$day,"/",$year,"'");
 	($year,$month,$day) = ymd_validate($year,$month,$day);
-	if($year)
-	{
+	if($year) {
 	    $fixeddate = $year;
 	    $fixeddate .= sprintf("-%02u",$month)
 		unless( ($month == 1) && ($day == 1) );
@@ -6074,12 +10897,10 @@ sub fix_datestring
     }
 
     debug(3,"DEBUG: checking M(M)/YYYY");
-    if(( ($month,$year) = /^(\d{1,2})\/(\d{4})$/x ) == 2)
-    {
+    if(( ($month,$year) = /^(\d{1,2})\/(\d{4})$/x ) == 2) {
 	# We have a XX/XXXX datestring
 	debug(3,"DEBUG: found '",$month,"/",$year,"'");
-	if($month <= 12)
-	{
+	if($month <= 12) {
 	    # We probably have MM/YYYY
 	    $fixeddate = sprintf("%04u-%02u",$year,$month);
 	    debug(3,"DEBUG: returning '",$fixeddate,"'");
@@ -6097,22 +10918,19 @@ sub fix_datestring
     ($year,$month,$day) = /^(\d{4})-(\d{2})-(\d{2})$/x;
     ($year,$month,$day) = ymd_validate($year,$month,$day);
 
-    if(!$year)
-    {
+    if(!$year) {
 	debug(3,"DEBUG: checking YYYYMMDD");
 	($year,$month,$day) = /^(\d{4})(\d{2})(\d{2})$/x;
 	($year,$month,$day) = ymd_validate($year,$month,$day);
     }
 
-    if(!$year)
-    {
+    if(!$year) {
 	debug(3,"DEBUG: checking YYYY-M(M)");
 	($year,$month) = /^(\d{4})-(\d{1,2})$/x;
 	($year,$month) = ymd_validate($year,$month,undef);
     }
 
-    if(!$year)
-    {
+    if(!$year) {
 	debug(3,"DEBUG: checking YYYY");
 	($year) = /^(\d{4})$/x;
     }
@@ -6127,8 +10945,7 @@ sub fix_datestring
     # See:
     # http://search.cpan.org/perldoc?Date::Manip#TIME_ZONES
 
-    if(!$year)
-    {
+    if(!$year) {
 	$date = ParseDate($datestring);
 	$year = UnixDate($date,"%Y");
 	$month = UnixDate($date,"%m");
@@ -6136,18 +10953,15 @@ sub fix_datestring
 	debug(2,"DEBUG: Date::Manip found '",UnixDate($date,"%Y-%m-%d"),"'");
     }
 
-    if($year)
-    {
+    if($year) {
 	# If we still have a $year, $month and $day either don't exist
 	# or are plausibly valid.
 	print {*STDERR} "DEBUG: found year=",$year," " if($debug >= 2);
 	$fixeddate = sprintf("%04u",$year);
-	if($month)
-	{
+	if($month) {
 	    print {*STDERR} "month=",$month," " if($debug >= 2);
 	    $fixeddate .= sprintf("-%02u",$month);
-	    if($day)
-	    {
+	    if($day) {
 		print {*STDERR} "day=",$day if($debug >= 2);
 		$fixeddate .= sprintf("-%02u",$day);
 	    }
@@ -6157,13 +10971,11 @@ sub fix_datestring
 	return $fixeddate if($fixeddate);
     }
 
-    if(!$year)
-    {
+    if(!$year) {
 	debug(3,"fix_date: didn't find a valid date in '",$datestring,"'!");
 	return;
     }
-    elsif($debug)
-    {
+    elsif($debug) {
 	print {*STDERR} "DEBUG: found ",sprintf("04u",$year);
 	print {*STDERR} sprintf("-02u",$month) if($month);
 	print {*STDERR} sprintf("-02u",$day),"\n" if($day);
@@ -6200,8 +11012,7 @@ The OPS container to parse.  Defaults to 'META-INF/container.xml'
 
 =cut
 
-sub get_container_rootfile
-{
+sub get_container_rootfile {
     my ($container) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6212,8 +11023,7 @@ sub get_container_rootfile
 
     $container = 'META-INF/container.xml' if(! $container);
 
-    if(-f $container)
-    {
+    if(-f $container) {
 	$twig->parsefile($container) or return;
 	$rootfile = $twig->root->first_descendant('rootfile');
 	return unless($rootfile);
@@ -6223,6 +11033,57 @@ sub get_container_rootfile
 }
 
 
+=head2 C<hashvalue_key_self(\%hash, $modifier)>
+
+Takes as an argument a hash reference and an optional modifier and
+inserts a new key for every value in that hash if no such key already
+exists.
+
+If the modifer is set to 'lc' or 'uc', the value is either lowercased
+or uppercased respectively before it is used as a key.
+
+Croaks if the first argument is not a hashref, or if an invalid
+modifier string is used.
+
+=cut
+
+sub hashvalue_key_self {
+    my ($hashref, $modifier) = @_;
+    my $subname = ( caller(0) )[3];
+    debug(4,"DEBUG[",$subname,"]");
+
+    if (not $hashref or not ref($hashref)) {
+        croak($subname,"(): first argument is not a reference");
+    }
+    elsif (!UNIVERSAL::isa($hashref,'HASH')) {
+        croak($subname,"(): first argument is not a hashref");
+    }
+
+    my %modifier_dispatch = (
+        'lc' => \&_lc,
+        'uc' => \&_uc,
+       );
+
+    if ($modifier and not $modifier_dispatch{$modifier}) {
+        croak($subname,"(): ",$modifier," is not a valid modifier string");
+    }
+
+    foreach my $value (values %{$hashref}) {
+        my $key;
+        if($modifier) {
+            $key = $modifier_dispatch{$modifier}($value);
+        }
+        else {
+            $key = $value;
+        }
+        if (! $hashref->{$key}) {
+            $hashref->{$key} = $value;
+        }
+    }
+    return;
+}
+
+
 =head2 C<hexstring($bindata)>
 
 Takes as an argument a scalar containing a sequence of binary bytes.
@@ -6231,8 +11092,7 @@ hexadecimal equivalent.  There is no leading "0x" on the string.
 
 =cut
 
-sub hexstring
-{
+sub hexstring {
     my $data = shift;
     my $subname = ( caller(0) )[3];
     debug(4,"DEBUG[",$subname,"]");
@@ -6244,8 +11104,7 @@ sub hexstring
     my $retval = '';
     my $pos = 0;
 
-    while($pos < length($data))
-    {
+    while($pos < length($data)) {
         $byte = unpack("C",substr($data,$pos,1));
         $retval .= sprintf("%02x",$byte);
         $pos++;
@@ -6275,8 +11134,7 @@ Returns 1 on success, undef otherwise.
 
 =cut
 
-sub print_memory
-{
+sub print_memory {
     my ($label) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6284,8 +11142,7 @@ sub print_memory
     my @mem;
     my $fh_procstatm;
 
-    if(!open($fh_procstatm,"<","/proc/$$/statm"))
-    {
+    if(!open($fh_procstatm,"<","/proc/$$/statm")) {
 	print "[",$label,"]: " if(defined $label);
 	print "Couldn't open /proc/$$/statm [$!]\n";
         return;
@@ -6342,8 +11199,7 @@ metadata was found.
 
 =cut
 
-sub split_metadata
-{
+sub split_metadata {
     my ($metahtmlfile,$metafile) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6369,15 +11225,13 @@ sub split_metadata
           "  html='",$htmlfile,"'");
 
     # Move existing output files to avoid overwriting them
-    if(-f $metafile)
-    {
+    if(-f $metafile) {
         debug(3, "DEBUG: moving metafile '",$metafile,"'");
         croak ($subname,"(): output file '",$metafile,
                "' exists and could not be moved!")
             if(! rename($metafile,"$metafile.backup") );
     }
-    if(-f $htmlfile)
-    {
+    if(-f $htmlfile) {
         debug(3, "DEBUG: moving htmlfile '",$htmlfile,"'");
         croak ($subname,"(): output file '",$htmlfile,
                "' exists and could not be moved!")
@@ -6404,8 +11258,7 @@ sub split_metadata
     # Since multiple <metadata> sections may be present, cannot use
     # </metadata> as a delimiter.
     local $/;
-    while(<$fh_metahtml>)
-    {
+    while(<$fh_metahtml>) {
         s/\sfilepos=\d+//gix;
 	(@metablocks) = m#(<metadata>.*</metadata>)#gisx;
 	(@guideblocks) = m#(<guide>.*</guide>)#gisx;
@@ -6425,8 +11278,7 @@ sub split_metadata
     close($fh_metahtml)
         or croak($subname,"(): Failed to close '",$metahtmlfile,"'!");
 
-    if( (-z $htmlfile) && (-z $metafile) )
-    {
+    if( (-z $htmlfile) && (-z $metafile) ) {
         croak($subname,"(): ended up with no text in any output file",
               " -- bailing out!");
     }
@@ -6439,21 +11291,18 @@ sub split_metadata
     undef(@guideblocks);
     undef($_);
 
-    if(-z $htmlfile)
-    {
+    if(-z $htmlfile) {
         debug(1,"split_metadata(): HTML has zero size.",
              "  Not replacing original.");
         unlink($htmlfile);
     }
-    else
-    {
+    else {
         rename($htmlfile,$metahtmlfile)
             or croak("split_metadata(): Failed to rename ",$htmlfile,
                      " to ",$metahtmlfile,"!\n");
     }
 
-    if(-z $metafile)
-    {
+    if(-z $metafile) {
         croak($subname,
               "(): unable to remove empty output file '",$metafile,"'!")
             if(! unlink($metafile) );
@@ -6477,8 +11326,7 @@ Returns a list containing all filenames created.
 
 =cut
 
-sub split_pre
-{
+sub split_pre {
     my ($htmlfile,$outfilebase) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6578,8 +11426,7 @@ well.
 
 =cut
 
-sub strip_script
-{
+sub strip_script {
     my %args = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6592,8 +11439,7 @@ sub strip_script
         'outfile' => 1,
         'noscript' => 1,
         );
-    foreach my $arg (keys %args)
-    {
+    foreach my $arg (keys %args) {
         croak($subname,"(): invalid argument '",$arg,"'")
             if(!$valid_args{$arg});
     }
@@ -6626,6 +11472,60 @@ sub strip_script
 }
 
 
+=head2 C<system_result($caller,$retval,@syscmd)>
+
+Checks the result of a system call and croak on failure with an
+appropriate message.  For this to work, it MUST be used as the line
+immediately following the system command.
+
+=head3 Arguments
+
+=over
+
+=item $caller
+
+The calling function (used in output message)
+
+=item $retval
+
+The return value of the system command
+
+=item @syscmd
+
+The array passed to the system call
+
+=back
+
+=head3 Return Values
+
+Returns 0 on success
+
+Croaks on failure.
+
+=cut
+
+sub system_result {
+    my ($caller,$retval,@syscmd) = @_;
+
+    if ( ($CHILD_ERROR >> 8) == 0 ) {
+        return 0;
+    }
+    elsif ($CHILD_ERROR == -1) {
+        croak($caller," child failed to execute (ERRNO=",$ERRNO,"):\n ",
+              join(' ',@syscmd),"\n")
+    }
+    elsif ($CHILD_ERROR & 127) {
+        my $withcoredump = ($CHILD_ERROR & 128) ? 'with' : 'without';
+        croak($caller," child died with signal ",($CHILD_ERROR & 127)," ",
+              $withcoredump," coredump:\n ",join(' ',@syscmd),"\n");
+    }
+    else {
+        croak($caller," child exited with value ",$CHILD_ERROR >> 8,":\n ",
+              join(' ',@syscmd),"\n")
+    }
+}
+
+
 =head2 C<system_tidy_xhtml($infile,$outfile)>
 
 Runs tidy on a XHTML file semi-safely (using a secondary file)
@@ -6685,8 +11585,7 @@ Returns the return value from tidy
 
 =cut
 
-sub system_tidy_xhtml
-{
+sub system_tidy_xhtml {
     my ($infile,$outfile) = @_;
     my $retval;
 
@@ -6705,7 +11604,7 @@ sub system_tidy_xhtml
 		     '-asxhtml',
                      '--output-xhtml','yes',
                      '--add-xml-decl','yes',
-		     '--doctype','transitional',
+		     '--doctype','auto',
 		     '-f',$tidyxhtmlerrors,
 		     '-o',$outfile,
 		     $infile);
@@ -6795,55 +11694,52 @@ Returns the return value from tidy
 
 =cut
 
-sub system_tidy_xml
-{
-    my ($infile,$outfile) = @_;
-    my $retval;
-
-    croak("system_tidy_xml called with no input file") if(!$infile);
-
-    if(!$outfile)
-    {
-        my ($filebase,$filedir,$fileext) = fileparse($infile,'\.\w+$');
-        $outfile = $filebase . "-tidy" . $fileext;
-    }
-    croak("system_tidy_xml called with no output file") if(!$outfile);
-
-    $retval = system($tidycmd,
-		     '-q','-utf8','--tidy-mark','no',
-                     '--wrap','0',
-		     '-xml',
-                     '--add-xml-decl','yes',
-		     '-f',$tidyxmlerrors,
-		     '-o',$outfile,
-		     $infile);
-
-    # Some systems may return a two-byte code, so deal with that first
-    if($retval >= 256) { $retval = $retval >> 8 };
-    if($retval == 0)
-    {
-	rename($outfile,$infile) if($tidysafety < 4);
-	unlink($tidyxmlerrors);
-    }
-    elsif($retval == 1)
-    {
-	rename($outfile,$infile) if($tidysafety < 3);
-	unlink($tidyxmlerrors) if($tidysafety < 2);
-    }
-    elsif($retval == 2)
-    {
-	print STDERR "WARNING: Tidy errors encountered.  Check ",$tidyxmlerrors,"\n"
-	    if($tidysafety > 0);
-	unlink($tidyxmlerrors) if($tidysafety < 1);
-    }
-    else
-    {
-	# Something unexpected happened (program crash, sigint, other)
-	croak("Tidy did something unexpected (return value=",$retval,
-              ").  Check all output.");
-    }
-    return $retval;
-}
+  sub system_tidy_xml {
+      my ($infile,$outfile) = @_;
+      my $retval;
+
+      croak("system_tidy_xml called with no input file") if(!$infile);
+
+      if (!$outfile) {
+          my ($filebase,$filedir,$fileext) = fileparse($infile,'\.\w+$');
+          $outfile = $filebase . "-tidy" . $fileext;
+      }
+      croak("system_tidy_xml called with no output file") if(!$outfile);
+
+      $retval = system($tidycmd,
+                       '-q','-utf8','--tidy-mark','no',
+                       '--wrap','0',
+                       '-xml',
+                       '--add-xml-decl','yes',
+                       '-f',$tidyxmlerrors,
+                       '-o',$outfile,
+                       $infile);
+
+      # Some systems may return a two-byte code, so deal with that first
+      if ($retval >= 256) {
+          $retval = $retval >> 8;
+      }
+      ;
+      if ($retval == 0) {
+          rename($outfile,$infile) if($tidysafety < 4);
+          unlink($tidyxmlerrors);
+      }
+      elsif ($retval == 1) {
+          rename($outfile,$infile) if($tidysafety < 3);
+          unlink($tidyxmlerrors) if($tidysafety < 2);
+      }
+      elsif ($retval == 2) {
+          print STDERR "WARNING: Tidy errors encountered.  Check ",$tidyxmlerrors,"\n"
+	    if ($tidysafety > 0);
+          unlink($tidyxmlerrors) if($tidysafety < 1);
+      }
+      else {
+          # Something unexpected happened (program crash, sigint, other)
+          croak("Tidy did something unexpected (return value=",$retval,
+                ").  Check all output.");
+      }
+      return $retval;
+  }
 
 
 =head2 C<trim>
@@ -6867,9 +11763,10 @@ String::Strip.
 
 =cut
 
-sub trim   ## no critic (Always unpack @_ first)
-{
-    ## no critic (Comma used to separate statements)
+sub trim { ## no critic
+    ## no critic
+    # PerlCritic is turned off here, as this is unusual code
+    # deliberately bending rules.
     @_ = $_ if not @_ and defined wantarray;
     @_ = @_ if defined wantarray;
     for ( @_ ? @_ : $_ ) { s/^\s+//, s/\s+$// }
@@ -6896,8 +11793,7 @@ Returns the element.
 
 =cut
 
-sub twigelt_create_uuid
-{
+sub twigelt_create_uuid {
     my ($gi) = @_;
     my $subname = ( caller(0) )[3];
     debug(2,"DEBUG[",$subname,"]");
@@ -6926,32 +11822,28 @@ Croaks if passed anything but twig elements.
 
 =cut
 
-sub twigelt_detect_duplicate
-{
+sub twigelt_detect_duplicate {
     my ($element1,$element2) = @_;
     my $subname = ( caller(0) )[3];
     debug(3,"DEBUG[",$subname,"]");
 
     croak($subname,"(): arguments must be XML::Twig::Elt objects")
-        unless( $element1->isa('XML::Twig::Elt')
+      unless( $element1->isa('XML::Twig::Elt')
                 && $element2->isa('XML::Twig::Elt') );
 
     my (%atts1, %atts2);
 
-    unless($element1->cmp($element2))
-    {
+    unless($element1->cmp($element2)) {
         debug(3,"  both elements have the same position");
         return 0;
     }
 
-    unless( lc($element1->gi) eq lc($element2->gi) )
-    {
+    unless( lc($element1->gi) eq lc($element2->gi) ) {
         debug(3,"  elements have different GIs");
         return 0;
     }
 
-    unless($element1->text eq $element2->text)
-    {
+    unless($element1->text eq $element2->text) {
         debug(3,"  elements have different text");
         return 0;
     }
@@ -6962,21 +11854,13 @@ sub twigelt_detect_duplicate
     my $attkeys1 = join('',sort keys %atts1);
     my $attkeys2 = join('',sort keys %atts2);
 
-    # This is much simpler with the ~~ operator, but Perl 5.10
-    # features are being avoided until 5.10 is standard both on MacOSX
-    # and Debian
-    # Note that the ~~ operator only checks keys of hashes, not values
-#    unless(%atts1 ~~ %atts2)
-    unless($attkeys1 eq $attkeys2)
-    {
+    unless($attkeys1 eq $attkeys2) {
         debug(3,"  elements have different attributes");
         return 0;
     }
 
-    foreach my $att (keys %atts1)
-    {
-        unless($element1->att($att) eq $element2->att($att))
-        {
+    foreach my $att (keys %atts1) {
+        unless($element1->att($att) eq $element2->att($att)) {
             debug(3,"  elements have different values for attribute '",
                   $att,"'");
             return 0;
@@ -7000,8 +11884,7 @@ opf:att exist.
 
 =cut
 
-sub twigelt_fix_oeb12_atts
-{
+sub twigelt_fix_oeb12_atts {
     my ($element) = @_;
     return unless($element);
     my $subname = ( caller(0) )[3];
@@ -7048,12 +11931,11 @@ element didn't exist.
 
 =cut
 
-sub twigelt_fix_opf20_atts
-{
+sub twigelt_fix_opf20_atts {
     my ($element) = @_;
     return unless($element);
     my $subname = ( caller(0) )[3];
-    debug(2,"DEBUG[",$subname,"]");
+    debug(3,"DEBUG[",$subname,"]");
 
     my %opfatts_ns = (
         "role" => "opf:role",
@@ -7101,8 +11983,7 @@ Intended to be used as a twig search condition.
 
 =cut
 
-sub twigelt_is_author
-{
+sub twigelt_is_author {
     my ($element) = @_;
     my $subname = ( caller(0) )[3];
     debug(3,"DEBUG[",$subname,"]");
@@ -7146,8 +12027,7 @@ Intended to be used as a twig search condition.
 
 =cut
 
-sub twigelt_is_isbn
-{
+sub twigelt_is_isbn {
     my ($element) = @_;
     my $subname = ( caller(0) )[3];
     debug(3,"DEBUG[",$subname,"]");
@@ -7193,8 +12073,7 @@ Intended to be used as a twig search condition.
 
 =cut
 
-sub twigelt_is_knownuid
-{
+sub twigelt_is_knownuid {
     my ($element) = @_;
     my $subname = ( caller(0) )[3];
     debug(3,"DEBUG[",$subname,"]");
@@ -7243,17 +12122,15 @@ Croaks on any failure.
 
 =cut
 
-sub usedir
-{
+sub usedir {
     my ($dir) = @_;
     my $subname = ( caller(0) )[3];
-    debug(2,"DEBUG[",$subname,"]");
+    debug(2,"DEBUG[",$subname,"]: $dir");
 
     my $cwd = getcwd();
     return $cwd unless($dir);
 
-    unless(-d $dir)
-    {
+    unless(-d $dir) {
         debug(2,"  Creating directory '",$dir,"'");
         mkpath($dir)
             or croak("Unable to create output directory '",$dir,"'!\n");
@@ -7273,30 +12150,28 @@ but on MSWin32 systems if that directory does not already exist,
 C<"$ENV{USERPROFILE}/ApplicationData/EBook-Tools"> is returned (and
 potentially created) instead.
 
-If C<$ENV{HOME}> (and C<$ENV{USERPROFILE}> on MSWin32) are not set, the
-sub returns undef.
+If C<$ENV{HOME}> (and C<$ENV{USERPROFILE}> on MSWin32) are either not
+set or do not point to a directory, the sub returns undef.
 
 =cut
 
-sub userconfigdir
-{
+sub userconfigdir {
     my $subname = ( caller(0) )[3];
     debug(3,"DEBUG[",$subname,"]");
 
     my $dir;
-    $dir = $ENV{HOME} . '/.ebooktools' if($ENV{HOME});
-    if($OSNAME eq 'MSWin32')
-    {
-        if(! -d $dir)
-        {
-            $dir = $ENV{USERPROFILE} . '\Application Data\EBook-Tools'
-                if($ENV{USERPROFILE});
+
+    if(-d $ENV{HOME}) {
+        $dir = $ENV{HOME} . '/.ebooktools';
+    }
+
+    if($OSNAME eq 'MSWin32') {
+        if(not -d $dir and -d $ENV{USERPROFILE}) {
+            $dir = $ENV{USERPROFILE} . '\Application Data\EBook-Tools';
         }
     }
-    if($dir)
-    {
-        if(! -d $dir)
-        {
+    if($dir) {
+        if(! -d $dir) {
             mkpath($dir)
                 or croak($subname,
                          "(): unable to create configuration directory '",
@@ -7316,19 +12191,15 @@ skipped by passing undef in that spot.
 
 =cut
 
-sub ymd_validate
-{
+sub ymd_validate {
     my ($year,$month,$day) = @_;
 
     return (undef,undef,undef) unless($year);
 
-    if($month)
-    {
+    if($month) {
 	return (undef,undef,undef) if($month > 12);
-	if($day)
-	{
-	    if(!eval { timelocal(0,0,0,$day,$month-1,$year); })
-	    {
+	if($day) {
+	    if(!eval { timelocal(0,0,0,$day,$month-1,$year); }) {
 		debug(1,"DEBUG: timelocal validation failed for year=",
                       $year," month=",$month," day=",$day);
 		return (undef,undef,undef);
@@ -7350,16 +12221,14 @@ sub ymd_validate
 
 =over
 
-=item * need to implement fix_primary_author() to convert names to
-standard 'last, first' naming format
-
 =item * fix_links() could be improved to download remote URIs instead
 of ignoring them.
 
 =item * fix_links() needs to check the <reference> links under <guide>
 
-=item * fix_links() needs to be redone with HTML::TreeBuilder to avoid
-the weakness with newlines between attribute names and values
+=item * fix_links() needs to be redone with HTML::TreeBuilder or
+Mojo::DOM to avoid the weakness with newlines between attribute names
+and values
 
 =item * Need to implement fix_tours() that should collect the related
 elements and delete the parent if none are found.  Empty <tours>
@@ -7401,7 +12270,7 @@ Zed Pobre <zed@debian.org>
 
 =head1 LICENSE AND COPYRIGHT
 
-Copyright 2008 Zed Pobre
+Copyright 2008-2013 Zed Pobre
 
 Licensed to the public under the terms of the GNU GPL, version 2
 
@@ -1,5 +1,6 @@
 #!/usr/bin/perl
 use warnings; use strict;
+use v5.10.1;
 
 
 =head1 NAME
@@ -14,16 +15,17 @@ See also L</EXAMPLES>.
 
 =cut
 
-
 use Config::IniFiles;
+use Cwd qw(chdir getcwd realpath);
 use EBook::Tools qw(:all);
 use EBook::Tools::IMP qw(:all);
 use EBook::Tools::Mobipocket qw(:all);
 use EBook::Tools::MSReader qw(:all);
 use EBook::Tools::Unpack;
-use File::Basename 'fileparse';
+use File::Basename qw(dirname fileparse);
 use File::Path;              # Exports 'mkpath' and 'rmtree'
 use File::Slurp qw(slurp);   # Also exports 'read_file' and 'write_file'
+use File::Temp qw(tempfile tempdir);
 use Getopt::Long qw(:config bundling);
 
 # Exit values
@@ -66,8 +68,11 @@ my %opt = (
     'category'    => undef,
     'compression' => undef,
     'dir'         => '',
+    'erotic'      => 0,
     'fileas'      => '',
     'firstname'   => undef,
+    'fix'         => 1,
+    'format'      => '',
     'help'        => 0,
     'htmlconvert' => 0,
     'identifier'  => undef,
@@ -78,6 +83,7 @@ my %opt = (
     'mimetype'    => '',
     'mobi'        => 0,
     'mobigencmd'  => $config->val('helpers','mobigen') || undef,
+    'names'	  => 0,
     'nosave'      => 0,
     'noscript'    => 0,
     'oeb12'       => 0,
@@ -97,9 +103,13 @@ GetOptions(
     'author=s',
     'category|cat=s',
     'compression|c=i',
+    'delete|del',
     'dir|d=s',
+    'erotic|sex',
     'fileas=s',
     'firstname=s',
+    'fix',
+    'format=s',
     'help|h|?',
     'htmlconvert',
     'identifier|id=s',
@@ -110,6 +120,7 @@ GetOptions(
     'mimetype|mtype=s',
     'mobi|m',
     'mobigencmd|mobigen=s',
+    'names',
     'nosave',
     'noscript',
     'raw',
@@ -131,8 +142,8 @@ if($opt{oeb12} && $opt{opf20})
     exit(EXIT_BADOPTION);
 }
 
-# Default to OEB12 if neither format is specified
-if(!$opt{oeb12} && !$opt{opf20}) { $opt{oeb12} = 1; }
+# Default to OPF20 if neither format is specified
+if(!$opt{oeb12} && !$opt{opf20}) { $opt{opf20} = 1; }
 
 $EBook::Tools::debug = $opt{verbose};
 $EBook::Tools::tidycmd = $opt{tidycmd} if($opt{tidycmd});
@@ -146,22 +157,27 @@ $EBook::Tools::tidysafety = $opt{tidysafety} if(defined $opt{tidysafety});
 my %dispatch = (
     'adddoc'      => \&adddoc,
     'additem'     => \&additem,
+    'bisac'       => \&bisac,
     'blank'       => \&blank,
+    'convert'     => \&convert,
     'dc'          => \&downconvert,
+    'dlbisac'     => \&dlbisac,
     'downconvert' => \&downconvert,
     'config'      => \&config,
     'fix'         => \&fix,
     'genepub'     => \&genepub,
     'genimp'      => \&genimp,
     'genmobi'     => \&genmobi,
+    'genncx'	  => \&genncx,
     'impmeta'     => \&impmeta,
+    'setcover'    => \&setcover,
     'setmeta'     => \&setmeta,
     'splitmeta'   => \&splitmeta,
     'splitpre'    => \&splitpre,
     'stripscript' => \&stripscript,
     'tidyxhtml'   => \&tidyxhtml,
     'tidyxml'     => \&tidyxml,
-    'unpack'      => \&unpack,
+    'unpack'      => \&unpack_ebook,
     );
 
 my $cmd = shift;
@@ -180,6 +196,7 @@ if(!$dispatch{$cmd})
 }
 
 $dispatch{$cmd}(@ARGV);
+exit(EXIT_SUCCESS);
 
 
 #########################################
@@ -243,7 +260,7 @@ sub adddoc
     }
     $ebook->save;
     $ebook->print_warnings;
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -312,7 +329,42 @@ sub additem
     }
     $ebook->save;
     $ebook->print_warnings;
-    exit(EXIT_SUCCESS);
+    return 0;
+}
+
+
+=head2 C<bisac>
+
+Search for a BISAC code matching a case-insensitive regular expression.
+
+=head3 Options
+
+=over
+
+=item C<regexp>
+
+The first argument is taken as a regular expression to use for the
+search.  If this is either '.' or not specified, the entire list of
+valid codes is printed.
+
+This requires that the BISAC codes be downloaded ahead of time.  (See
+C<dlbisac>.)
+
+=back
+
+=cut
+
+sub bisac
+{
+    my ($regexp) = @_;
+    my $bisg = EBook::Tools::BISG->new();
+
+    my @list = $bisg->find($regexp);
+
+    foreach my $code (@list) {
+        print $code,"\n";
+    }
+    return 0;
 }
 
 
@@ -369,14 +421,11 @@ sub blank
     $args{author} = $opt{author} if($opt{author});
     $args{title} = $opt{title} if($opt{title});
 
+    useoptdir() unless($opt{nosave});
     $ebook = EBook::Tools->new();
     $ebook->init_blank(%args);
-    unless($opt{nosave})
-    {
-        useoptdir();
-        $ebook->save;
-    }
-    exit(EXIT_SUCCESS);
+    $ebook->save unless($opt{nosave});
+    return 0;
 }
 
 
@@ -521,7 +570,89 @@ sub config
         $config->setval('drm','mobipids',$value);
         $config->RewriteConfig;
     }
-    exit(EXIT_SUCCESS);
+    return 0;
+}
+
+
+=head2 C<convert>
+
+Unpacks the ebook specified as the first argument, runs standard fixes
+on the contents, and repacks it into a new format in the output file
+specified as the second argument.  Currently the only supported output
+format is epub, which is the format you will get irrespective of the
+extension you actually give the output file.
+
+=head3 Options
+
+=over
+
+All options from C<unpack> and C<fix> are technically valid here as
+well, though of course some options are nonsensical in this context
+and will likely break the conversion (e.g. --nosave).
+
+=back
+
+=head3 Example
+
+ ebook convert MyBook.prc MyBook.epub
+ ebook convert --name MyBook.lit /home/myname/MyBook.epub
+
+=cut
+
+sub convert
+{
+    my ($infile,$outfile) = @_;
+    my $subname = ( caller(0) )[3];
+
+    my $tempdir = File::Temp->newdir();
+    $tempdir->unlink_on_destroy( 1 );
+    my $cwd_orig = getcwd();
+
+    if(!$infile) { die("You must specify an input file.\n"); }
+    if(!$outfile) { die("You must specify an output file.\n"); }
+
+    my $ebook;
+    my $bookname;
+    my $format;
+
+    ($bookname,undef,undef) = fileparse($infile,'\.\w+$');
+    (undef,undef,$format) = fileparse($outfile,'\.\w+$');
+
+    # TODO: actually check $format
+
+    $infile = realpath($infile);
+    $outfile = realpath($outfile);
+
+    $dispatch{'unpack'}($infile,$tempdir);
+
+    chdir($tempdir);
+
+    $dispatch{'fix'}();
+
+    $opt{output} = $outfile;
+    $dispatch{'genepub'}();
+
+    chdir($cwd_orig);
+    return 0;
+}
+
+
+=head2 C<dlbisac>
+
+Downloads and caches the Book Industry Study Group BISAC codes into a
+local database.  This will destroy the existing contents of that table
+if this has been done previously.
+
+=cut
+
+sub dlbisac
+{
+    my $subname = ( caller(0) )[3];
+
+    my $bisg = EBook::Tools::BISG->new();
+    $bisg->download_bisac;
+    $bisg->save_bisac;
+    return;
 }
 
 
@@ -648,7 +779,7 @@ END
             {
                 print("Successfully downconverted '",$infile,
                     "' into '",$outfile,"'\n");
-                exit(EXIT_SUCCESS);
+                return 0;
             }
             else
             {
@@ -681,7 +812,7 @@ END
         {
             print("Successfully downconverted '",$infile,
                 "' into '",$outfile,"'\n");
-            exit(EXIT_SUCCESS);
+            return 0;
         }
         else
         {
@@ -695,7 +826,7 @@ END
         print {*STDERR} "Cannot downconvert format '",$unpacker->format,"'\n";
         exit(EXIT_BADINPUT);
     }
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -731,13 +862,18 @@ C<--oeb12> are specified, the program will abort with an error.
 Correct Mobipocket-specific elements, creating an output element to
 force UTF-8 output if one does not yet exist.
 
-=item C<--dir directory>
+=item C<--erotic> or C<--sex>
 
-=item C<-d directory>
+Enable special handling for erotic fiction (most notably special
+subject normalization rules).
 
-Save the fixed output into the specified directory.  The default is to
-write all output in the current working directory.  Note that this
-only affects the output, and not where the OPF file is found.
+=item C<--names>
+
+Normalize names to standard capitalization and format (primary name
+display is "First Middle Last", but file-as is "Last, First Middle".
+
+This is not done by default as it can damage unusual but correct
+names.
 
 =back
 
@@ -755,24 +891,31 @@ sub fix
     $opffile = $opt{opffile} if(!$opffile);
     $ebook = EBook::Tools->new();
     $ebook->init($opffile);
+    if($opt{erotic}) { $ebook->set_erotic(1); }
     $ebook->fix_oeb12 if($opt{oeb12});
-    $ebook->fix_opf20 if($opt{opf20});
+    if($opt{opf20}) {
+        if(-f 'META-INF/container.xml') {
+            $ebook->gen_epub_files;
+        }
+        else {
+            $ebook->fix_opf20;
+        }
+    }
     $ebook->fix_misc;
     $ebook->fix_mobi if($opt{mobi});
-    unless($opt{nosave})
-    {
-        useoptdir();
-        $ebook->save;
+    if($opt{names}) {
+        $ebook->fix_creators;
     }
-    if($ebook->errors)
-    {
+    $ebook->save unless($opt{nosave});
+
+    if($ebook->errors) {
         $ebook->print_errors;
         print "Unrecoverable errors while fixing '",$opffile,"'!\n";
         exit(EXIT_TOOLSERROR);
     }
 
     $ebook->print_warnings if($ebook->warnings);
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -792,17 +935,17 @@ Generate a .epub book from existing OPF data.
 
 =item C<--opf filename.opf>
 
-Use the specified OPF file.  This can also be specified as the first
-non-option argument, which will override this option if it exists.  If
-no file is specified, one will be searched for.
+Use the specified OPF file.  If no file is specified, one will be
+searched for.
 
 =item C<--output bookname.epub>
 
 =item C<-o bookname.epub>
 
-Use the specified name for the final output file.  If not specified,
-the bok will have the same filename as the OPF file, with the
-extension changed to C<.epub>.
+Use the specified name for the final output file.  This can also be
+specified as the first non-option argument, which will override this
+option if it exists.  If not specified, the book will have the same
+filename as the OPF file, with the extension changed to C<.epub>.
 
 =item C<--dir directory>
 
@@ -815,7 +958,7 @@ is to use the current working directory.
 
 =head3 Example
 
- ebook genepub mybook.opf -f my_special_book.epub -d ../epubbooks
+ ebook genepub mybook.opf -o my_special_book.epub -d ../epubbooks
 
 or in the simplest case:
 
@@ -825,16 +968,20 @@ or in the simplest case:
 
 sub genepub
 {
-    my ($opffile) = @_;
+    my ($outfile) = @_;
     my $ebook;
 
-    $opffile = $opt{inputfile} if(!$opffile);
-    $opffile = $opt{opffile} if(!$opffile);
+    if(!$outfile) { $outfile = $opt{output}; }
 
-    if($opffile) { $ebook = EBook::Tools->new($opffile); }
-    else {$ebook = EBook::Tools->new(); $ebook->init(); }
+    if($opt{opffile}) {
+        $ebook = EBook::Tools->new($opt{opffile});
+    }
+    else {
+        $ebook = EBook::Tools->new();
+        $ebook->init();
+    }
 
-    if(! $ebook->gen_epub(filename => $opt{filename},
+    if(! $ebook->gen_epub(filename => $opt{output},
                           dir => $opt{dir}) )
     {
         print {*STDERR} "Failed to generate .epub file!\n";
@@ -843,7 +990,7 @@ sub genepub
         exit(EXIT_BADOUTPUT);
     }
     $ebook->print_warnings if($ebook->warnings);
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -924,7 +1071,7 @@ sub genimp
         print {*STDERR} ("Failed to generate '",$output,"'!\n");
         exit(EXIT_BADOUTPUT);
     }
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -1021,7 +1168,58 @@ sub genmobi
         print {*STDERR} "Error during generation: ",$retval,"\n";
         exit(EXIT_HELPERERROR);
     }
-    exit(EXIT_SUCCESS);
+    return 0;
+}
+
+
+=head2 C<genncx>
+
+Given an OPF file, creates a NCX-format table of contents from the
+package unique-identifier, the dc:title, dc:creator, and spine
+elements, and then add the NCX entry to the manifest if it is not
+already referenced.
+
+The OPF file will be cleaned to OPF20 format before this happens.
+
+=head3 Options
+
+=over
+
+=item C<--opffile filename.opf>
+
+=item C<--opf filename.opf>
+
+Use the specified OPF file.  This can also be specified as the first
+non-option argument, which will override this option if it exists.  If
+no file is specified, one will be searched for.
+
+=back
+
+=cut
+
+sub genncx {
+    my ($opffile) = @_;
+    my $ebook;
+
+    $opffile = $opt{opffile} if(!$opffile);
+    $ebook = EBook::Tools->new();
+    $ebook->init($opffile);
+    $ebook->fix_opf20;
+    $ebook->gen_ncx;
+    unless($opt{nosave})
+    {
+        useoptdir();
+        $ebook->save;
+    }
+    if($ebook->errors)
+    {
+        $ebook->print_errors;
+        print "Unrecoverable errors while fixing '",$opffile,"'!\n";
+        exit(EXIT_TOOLSERROR);
+    }
+
+    $ebook->print_warnings if($ebook->warnings);
+    return 0;
 }
 
 
@@ -1125,20 +1323,99 @@ sub impmeta
         print "Failed to write '",$output,"' -- aborting!\n";
         exit(EXIT_BADOUTPUT);
     }
-    exit(EXIT_SUCCESS);
+    return 0;
+}
+
+=head2 C<setcover>
+
+Sets the cover image
+
+Takes as a single argument the href of the file to use.
+
+=head3 Options
+
+=over
+
+=item * C<--opffile>
+
+=item * C<--opf>
+
+Specifies the OPF file to modify.  If not specified, the script will
+attempt to find one
+
+=item * C<--identifier>
+
+=item * C<--id>
+
+Specifies the ID to assign to the associated manifest item.
+
+=back
+
+=cut
+
+sub setcover {
+    my ($href) = @_;
+
+    unless($href) {
+        print "You must specify the href (filename) of the cover image!\n";
+        exit(EXIT_BADOPTION);
+    }
+
+    my $opffile = $opt{opffile};
+    my $id = $opt{identifier};
+
+    my $ebook = EBook::Tools->new();
+    $ebook->init($opffile);
+    $ebook->set_cover(href => $href,
+                      id => $id);
+    $ebook->save;
+    $ebook->print_errors;
+    $ebook->print_warnings;
+    return 0;
 }
 
 
 =head2 C<setmeta>
 
-Set specific metadata values on an OPF file, creating a new entry only
-if none exists.
+Set specific metadata values on an OPF file, creating a new entry if
+none exists.
 
 Both the element to set and the value are specified as additional
 arguments, not as options.
 
-The elements that can be set are currently 'author', 'title',
-'publisher', and 'rights'.
+The elements that can be set are currently:
+
+=over
+
+=item author
+
+=item date
+
+=item description
+
+=item publisher
+
+=item rights
+
+=item series
+
+=item subject
+
+=item title
+
+=item type
+
+=back
+
+The 'series' values can take an extra argument containing the series
+index position.
+
+The 'subject' elements can be added multiple times (including in a
+single command-line, though --id will only set the ID on the first one
+specified).  Other entries will be overwritten.
+
+The element argument can be shortened to the minimum number of letters
+necessary to uniquely identify it.
 
 =head3 Options
 
@@ -1150,6 +1427,10 @@ The elements that can be set are currently 'author', 'title',
 Specifies the OPF file to modify.  If not specified, the script will
 attempt to find one in the current directory.
 
+=item * C<--delete>
+
+Allows the deletion of subject and series metadata.  Has no effect on other elements.
+
 =item * C<--fileas>
 
 Specifies the 'file-as' attribute when setting an author.  Has no
@@ -1165,20 +1446,16 @@ Specifies the ID to assign to the element.
 
 =head3 Examples
 
+ ebook setmeta series 'Some Other Series' 03
  ebook setmeta title 'My Great Title'
+ ebook setmeta t 'My Great Title'
  ebook --opf newfile.opf setmeta author 'John Smith' --fileas 'Smith, John' --id mainauthor
 
 =cut
 
 sub setmeta
 {
-    my ($element,$value) = @_;
-    my %valid_elements = (
-        'title' => 1,
-        'author' => 1,
-        'publisher' => 1,
-        'rights' => 1,
-        );
+    my ($element,$value,@extra) = @_;
 
     unless($element)
     {
@@ -1186,12 +1463,7 @@ sub setmeta
         print "Example: ebook setmeta title 'My Great Title'\n";
         exit(EXIT_BADOPTION);
     }
-    unless($value)
-    {
-        print "You muts specify the value to set.\n";
-        print "Example: ebook setmeta title 'My Great Title'\n";
-        exit(EXIT_BADOPTION);
-    }
+    $value ||= '';
 
     my $opffile = $opt{opffile};
     my $fileas = $opt{fileas};
@@ -1200,31 +1472,82 @@ sub setmeta
     my $ebook = EBook::Tools->new();
     $ebook->init($opffile);
 
-    if($element eq 'author')
-    {
+    if ($element =~ /^a/) {
         $ebook->set_primary_author('text' => $value,
                                    'fileas' => $fileas,
                                    'id' => $id );
     }
-    elsif($element eq 'publisher')
-    {
+    elsif ($element =~ /^da/) {
+        $value = fix_datestring($value);
+        $ebook->set_date('text' => $value,
+                         'id' => $id);
+    }
+    elsif ($element =~ /^de/) {
+        $ebook->set_description('text' => $value,
+                                'id' => $id);
+    }
+    elsif ($element =~ /^p/) {
         $ebook->set_publisher('text' => $value,
-                              'id' => $id );
+                              'id' => $id);
     }
-    elsif($element eq 'rights')
-    {
+    elsif ($element =~ /^r/) {
         $ebook->set_rights('text' => $value,
-                           'id' => $id );
+                           'id' => $id);
     }
-    elsif($element eq 'title')
-    {
+    elsif ($element =~ /^se/) {
+        # Unfortunately, there are no readers as of this writing
+        # that support the EPub 3.0 collection standard, so the
+        # only way to currently designate series is via the
+        # calibre:series and calibre:series_index meta tags.
+
+        if($opt{delete}) {
+            $ebook->set_meta(name => 'calibre:series',
+                             content => undef);
+            $ebook->set_meta(name => 'calibre:series_index',
+                             content => undef);
+        }
+        else {
+            $ebook->set_meta(name => 'calibre:series',
+                             content => $value);
+            if($extra[0]) {
+                $ebook->set_meta(name => 'calibre:series_index',
+                                 content => $extra[0]);
+            }
+        }
+    }
+    elsif ($element =~ /^su/) {
+        if($opt{delete}) {
+            $ebook->delete_subject('text' => $value,
+                                   'id' => $id);
+            foreach my $subject(@extra) {
+                $ebook->delete_subject('text' => $subject);
+            }
+        }
+        else {
+            $ebook->add_subject('text' => $value,
+                                'id' => $id);
+            foreach my $subject(@extra) {
+                $ebook->add_subject('text' => $subject);
+            }
+        }
+    }
+    elsif ($element =~ /^ti/) {
         $ebook->set_title('text' => $value,
                           'id' => $id);
     }
+    elsif ($element =~ /^ty/) {
+        $ebook->set_type('text' => $value,
+                         'id' => $id);
+    }
+    else {
+        print "Unrecognized metadata element '",$element,"'!\n";
+        exit(EXIT_BADOPTION);
+    }
+
     $ebook->save;
     $ebook->print_errors;
     $ebook->print_warnings;
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -1269,7 +1592,7 @@ sub splitmeta
         exit(EXIT_TOOLSERROR);
     }
     $ebook->print_warnings if($ebook->warnings);
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -1293,7 +1616,7 @@ sub splitpre
         exit(EXIT_BADOPTION);
     }
     split_pre($infile,$outfilebase);
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 
@@ -1332,7 +1655,7 @@ sub stripscript
     $args{noscript} = $opt{noscript};
 
     strip_script(%args);
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 =head2 C<tidyxhtml>
@@ -1382,7 +1705,7 @@ sub tidyxml
 }
 
 
-=head2 C<unpack>
+=head2 C<unpack_ebook>
 
 Unpacks an ebook into its component parts, creating an OPF for them if
 necessary.
@@ -1441,7 +1764,7 @@ exact text can be overridden here.
 =item C<--opf>
 
 The filename of the OPF metadata file that will be generated.  If not
-specified, defaults to the title with a .opf extension.
+specified, defaults to C<content.opf>.
 
 =item C<--tidy>
 
@@ -1497,7 +1820,7 @@ Both of the above commands do the same thing
 
 =cut
 
-sub unpack
+sub unpack_ebook
 {
     my ($filename,$dir) = @_;
     $filename = $filename || $opt{input};
@@ -1536,7 +1859,7 @@ sub unpack
 
     $unpacker->unpack;
 
-    exit(EXIT_SUCCESS);
+    return 0;
 }
 
 ########## PRIVATE PROCEDURES ##########
@@ -5,10 +5,29 @@
 
 # change 'tests => 1' to 'tests => last_test_to_print';
 
-use Test::More tests => 31;
+use Test::More tests => 32;
+
 # Add blib/scripts to the path to find the executables
 
-BEGIN { use_ok('EBook::Tools') };
+BEGIN {
+    use_ok('EBook::Tools');
+    use_ok('EBook::Tools::BISG');
+
+    if(not $ENV{'AUTOMATED_TESTING'})
+    {
+        # We have to ensure that the BISAC codes have been cached
+        # locally as early as possible before we begin, or the subject
+        # cleanup tests will fail in 20-Unpack.  Unfortunately, this
+        # fails on some automated test environments, so we have to
+        # skip it in both places.
+
+        my $bisg = EBook::Tools::BISG->new();
+        if(! $bisg->bisac() ) {
+            $bisg->download_bisac;
+            $bisg->save_bisac;
+        }
+    }
+};
 # $EBook::Tools::debug = 2;
 my $ebook;
 my ($meta1,$meta2);
@@ -65,7 +65,7 @@ is($ebook1->opffile,'missingfwid.opf',
 # init_blank()
 $blank = EBook::Tools->new() or die;
 ok($blank->init_blank(),'init_blank() returned successfully');
-is($blank->opffile,'t.opf','init_blank() created opf from directory name');
+is($blank->opffile,'content.opf','init_blank() created default opf filename');
 
 # init_blank(opffile => $filename)
 ok($blank->init_blank(opffile => 'test-blank.opf'),
@@ -3,7 +3,7 @@ use Cwd qw(chdir getcwd realpath);
 use File::Basename qw(basename dirname);
 use File::Copy;
 use File::Path;    # Exports 'mkpath' and 'rmtree'
-use Test::More tests => 11;
+use Test::More tests => 17;
 BEGIN
 {
     use_ok('EBook::Tools',qw(:all));
@@ -18,6 +18,15 @@ my $longline = 'We think ourselves possessed, or, at least, we boast that we are
 my $hexable = "\x{0}\x{d0}\x{be}\x{d0}\x{be}\x{da}";
 my $scriptname = basename($0);
 
+my %selfhash = (
+    '1'   => 'One',
+    '2'   => 'Two',
+    'One' => '1',
+    'one' => 'Zero',
+    'ONE' => 'Two',
+   );
+my %testhash;
+
 ########## TESTS BEGIN ##########
 
 ok( (basename(getcwd()) eq 't') || chdir('t/'), "Working in 't/" ) or die;
@@ -39,6 +48,21 @@ copy('testopf-missingfwid.xml','missingfwid.opf') or die("Could not copy: $!");
 is(find_opffile(),undef,
    'find_opffile() returns undef with multiple OPF files');
 
+%testhash = %selfhash;
+hashvalue_key_self(\%testhash);
+is($testhash{'One'},'1','hashvalue_key_self/undef did not modify existing key');
+is($testhash{'Two'},'Two','hashvalue_key_self/undef created self reference');
+
+%testhash = %selfhash;
+hashvalue_key_self(\%testhash,'lc');
+is($testhash{'one'},'Zero','hashvalue_key_self/lc did not modify existing key');
+is($testhash{'two'},'Two','hashvalue_key_self/lc created self reference');
+
+%testhash = %selfhash;
+hashvalue_key_self(\%testhash,'uc');
+is($testhash{'ONE'},'Two','hashvalue_key_self/uc did not modify existing key');
+is($testhash{'ZERO'},'Zero','hashvalue_key_self/uc created self reference');
+
 is(hexstring($hexable),'00d0bed0beda',
    'hexstring() returns correct string');
 
@@ -52,7 +76,7 @@ SKIP:
        'system_tidy_xml: missingfwid.opf');
 }
 
-SKIP: 
+SKIP:
 {
     skip('No /proc/PID/statm',1) unless(-f "/proc/$$/statm");
     ok(print_memory('15-misc_procedures.t'),'print_memory returns sucessfully');
@@ -2,23 +2,34 @@ use strict; use warnings; use utf8;
 use Cwd qw(chdir getcwd);
 use Digest::MD5 qw(md5_base64);
 use EBook::Tools;
+use EBook::Tools::BISG;
 use File::Basename qw(basename);
 use File::Copy;
 use File::Path;    # Exports 'mkpath' and 'rmtree'
-use Test::More tests => 48;
+use Test::More;
 BEGIN
 {
-    use_ok('EBook::Tools::Unpack');
-    use_ok('EBook::Tools::EReader',qw(:all));
-    use_ok('EBook::Tools::Mobipocket',qw(:all));
-    use_ok('EBook::Tools::MSReader',qw(:all));
-    use_ok('EBook::Tools::PalmDoc',qw(:all));
+    if($ENV{'AUTOMATED_TESTING'})
+    {
+        plan skip_all => "Accurate unpacker tests rely on the BISG downloads that some automated testers can't handle.";
+    }
+    else
+    {
+        plan tests => 49;
+        use_ok('EBook::Tools::Unpack');
+        use_ok('EBook::Tools::BISG');
+        use_ok('EBook::Tools::EReader',qw(:all));
+        use_ok('EBook::Tools::Mobipocket',qw(:all));
+        use_ok('EBook::Tools::MSReader',qw(:all));
+        use_ok('EBook::Tools::PalmDoc',qw(:all));
+    }
 };
 
 # Set this to 1 or 2 to stress the debugging code, but expect lots of
 # output.
 $EBook::Tools::debug = 0;
 
+
 my $cwd;
 my $ebook = EBook::Tools->new();
 my $unpacker;
@@ -32,6 +43,7 @@ my %md5sums = (
 my $mobitest_description =
     '<P>Description line 1 — <EM>emphasized</EM> </P> <P>Description line 2 — <STRONG>a bold move </STRONG></P>';
 
+
 ########## TESTS BEGIN ##########
 
 ok( (basename(getcwd()) eq 't') || chdir('t/'), "Working in 't/" ) or die;
@@ -45,76 +57,79 @@ ok($unpacker = EBook::Tools::Unpack->new(
 ok($unpacker->unpack,'unpack(mobi) returns successfully');
 chdir($cwd);
 ok(-d 'mobitest','unpack() created mobitest/');
-ok(-f 'mobitest/mobitest.opf','unpack() created mobitest/mobitest.opf');
-ok(-f 'mobitest/mobitest.html','unpack() created mobitest/mobitest.html');
+ok(-f 'mobitest/content.opf','unpack() created mobitest/content.opf');
+ok(-f 'mobitest/A Noncompliant OPF Test Sample.html',
+   'unpack() created mobitest/A Noncompliant OPF Test Sample.html');
 
-ok($ebook->init('mobitest/mobitest.opf'),'mobitest.opf parses');
+ok($ebook->init('mobitest/content.opf'),'content.opf parses');
 is($ebook->title,'A Noncompliant OPF Test Sample',
-   'mobitest.opf title is correct');
+   'content.opf title is correct');
 is($ebook->primary_author,'Zed 1 Pobre',
-   'mobitest.opf author is correct');
+   'content.opf author is correct');
 @list = $ebook->contributor_list;
 is_deeply(\@list, ['Me Myself'],
-          'mobitest.opf has correct contributors');
+          'content.opf has correct contributors');
 @list = $ebook->isbn_list;
 is_deeply(\@list, ['0-9999-XXXX-1'],
-          'mobitest.opf has correct ISBNs');
+          'content.opf has correct ISBNs');
 @list = $ebook->subject_list;
-is_deeply(\@list, ['Computing, Internet','Secondary subject','Education'],
-   'mobitest.opf has correct subjects');
+is_deeply(\@list, ['Computing, Internet','Secondary subject','Education',
+                   'COMPUTERS / General','EDUCATION / General','Computers'],
+          'content.opf has correct subjects');
 is($ebook->description,$mobitest_description,
-   'mobitest.opf has correct description');
+   'content.opf has correct description');
 @list = $ebook->publishers;
 is_deeply(\@list, ['CPAN'],
-          'mobitest.opf has correct publishers');
+          'content.opf has correct publishers');
 is($ebook->date_list(event => 'publication'),'2008-10',
-   'mobitest.opf has correct publication date');
+   'content.opf has correct publication date');
 is($ebook->languages,'en',
-   'mobitest.opf has correct language');
+   'content.opf has correct language');
 is($ebook->element_list(cond => 'DictionaryInLanguage'),'de-at',
-   'mobitest.opf has correct DictionaryInLanguage');
+   'content.opf has correct DictionaryInLanguage');
 is($ebook->element_list(cond => 'DictionaryOutLanguage'),'es-ar',
-   'mobitest.opf has correct DictionaryOutLanguage');
+   'content.opf has correct DictionaryOutLanguage');
 is($ebook->adult,'yes',
-   'mobitest.opf is flagged adult');
+   'content.opf is flagged adult');
 @list = $ebook->retailprice;
 is_deeply(\@list, ['1.23','USD'],
-          'mobitest.opf has correct SRP');
+          'content.opf has correct SRP');
 @list = $ebook->manifest_hrefs;
-is_deeply(\@list, ['mobitest.html'],
-          'mobitest.opf has correct manifest');
+is_deeply(\@list, ['A Noncompliant OPF Test Sample.html'],
+          'content.opf has correct manifest');
 @list = $ebook->spine_idrefs;
 is_deeply(\@list, ['text-main'],
-          'mobitest.opf has correct spine');
+          'content.opf has correct spine');
 
 
 ##### MOBIPOCKET HUFF/CDIC #####
+chdir($cwd);
 ok($unpacker = EBook::Tools::Unpack->new(
-       'file' => 'mobi/hufftest.mobi'),
+    'file' => 'mobi/hufftest.mobi'),
    'new(file => mobi/hufftest.mobi) returns successfully');
 ok($unpacker->unpack,'unpack(mobi/hufftest) returns successfully');
 chdir($cwd);
 ok(-d 'hufftest','unpack(mobi/hufftest.mobi) created hufftest/');
-ok(-f 'hufftest/hufftest.opf',
-   'unpack(mobi/hufftest.mobi) created hufftest/hufftest.opf');
-ok(-f 'hufftest/hufftest.html',
-   'unpack(mobi/hufftest.mobi) created hufftest/hufftest.html');
+ok(-f 'hufftest/content.opf',
+   'unpack(mobi/hufftest.mobi) created hufftest/content.opf');
+ok(-f 'hufftest/Space Encyclopedia HUFFCDIC test.html',
+   'unpack(mobi/hufftest.mobi) created hufftest/Space Encyclopedia HUFFCDIC test.html');
 ok(-f 'hufftest/Space_Encycl-_HUFFCDIC_test-0001.jpg',
    'unpack(mobi/hufftest.mobi) created image');
-ok($ebook->init('hufftest/hufftest.opf'),'mobitest.opf parses');
+ok($ebook->init('hufftest/content.opf'),'content.opf parses');
 is($ebook->title,'Space Encyclopedia (HUFF/CDIC test)',
-   'hufftest.opf title is correct');
+   'content.opf title is correct');
 is($ebook->primary_author,'Mobipocket',
-   'hufftest.opf author is correct');
+   'content.opf author is correct');
 
 ##### EREADER #####
+chdir($cwd);
 ok($unpacker = EBook::Tools::Unpack->new(
        'file' => 'ereader/ertest.pdb'),
    'new(file => ereader/ertest.pdb) returns successfully');
 ok($unpacker->unpack,'unpack(ereader) returns successfully');
-chdir($cwd);
 ok(-d 'ertest','unpack() created ertest/');
-ok(-f 'ertest/ertest.opf','created ertest/ertest.opf');
+ok(-f 'ertest/content.opf','created ertest/content.opf');
 ok(-f 'ertest/ertest.pml','created ertest/ertest.pml');
 ok(-d 'ertest/ertest_img','unpack() created ertest/ertest_img');
 open(my $fh_md5,'<:raw','ertest/ertest_img/resonance.png')
@@ -123,19 +138,20 @@ $md5->addfile($fh_md5);
 close($fh_md5);
 is($md5->b64digest,$md5sums{'resonance.png'},
    'unpack() created ertest/ertest_img/resonance.png with correct checksum');
-ok($ebook->init('ertest/ertest.opf'),'ertest.opf parses');
+ok($ebook->init('ertest/content.opf'),'content.opf parses');
 is($ebook->title,'eReader Test',
-   'ertest.opf title is correct');
+   'content.opf title is correct');
 is($ebook->primary_author,'Zed Pobre',
-   'ertest.opf author is correct');
+   'content.opf author is correct');
 @list = $ebook->publishers;
 is_deeply(\@list, ['CPAN'],
-          'ertest.opf has correct publishers');
+          'content.opf has correct publishers');
 is($ebook->rights,"Copyright \x{a9} 2008 Zed Pobre",
-   'ertest.opf has correct rights (in UTF-8)');
+   'content.opf has correct rights (in UTF-8)');
 
 ########## CLEANUP ##########
 
+chdir($cwd);
 rmtree('ertest');
 rmtree('hufftest');
 rmtree('mobitest');
@@ -9,7 +9,7 @@ BEGIN
     }
     else
     {
-        plan tests => 18;
+        plan tests => 16;
     }
 };
 use Cwd qw(chdir getcwd);
@@ -28,6 +28,7 @@ BEGIN { use_ok('EBook::Tools') };
 $EBook::Tools::debug = 1;
 
 ok( (basename(getcwd()) eq 't') || chdir('t/'), "Working in 't/" ) or die;
+my $cwd = getcwd();
 
 copy('test-containsmetadata.html','containsmetadata.html')
     or die("Could not copy containsmetadata.html: $!");
@@ -61,6 +62,7 @@ is($ebook->twigroot->first_descendant('dc:title')->text,'Testing Title',
 is($ebook->twigroot->first_descendant('dc:creator')->text,'Testing Author',
    'ebook blank created correct author');
 
+chdir($cwd);
 # ebook fix
 $exitval = system('perl','-I../lib','../scripts/ebook.pl','fix','emptyuid.opf');
 $exitval >>= 8;
@@ -69,20 +71,11 @@ ok(-f 'emptyuid.opf.backup','ebook fix created backup file');
 
 # ebook genepub
 $exitval = system('perl','-I../lib','../scripts/ebook.pl',
-                  'genepub','emptyuid.opf',
+                  'genepub','--opf','emptyuid.opf',
                   '--dir','epubdir');
 $exitval >>= 8;
 is($exitval,0,'ebook genepub exits successfully');
-ok(-f 'epubdir/emptyuid.epub','ebook genepub created the epub book');
-
-# ebook fix -d testdir
-$exitval = system('perl','-I../lib','../scripts/ebook.pl',
-                  'fix','emptyuid.opf',
-                  '-d','testdir');
-$exitval >>= 8;
-is($exitval,0,'ebook fix -d testdir exits successfully');
-ok(-f 'testdir/emptyuid.opf',
-   'ebook fix -d testdir created file in correct place');
+ok(-f 'epubdir/t.epub','ebook genepub created the epub book');
 
 # ebook splitmeta
 unlink('containsmetadata.opf');
@@ -112,6 +105,7 @@ unlink('missingfwid.opf');
 unlink('missingfwid.opf.backup');
 unlink('part1.html');
 unlink('part2.html');
+unlink('toc.ncx');
 rmtree('META-INF');
 rmtree('epubdir');
 rmtree('testdir');