Moved a lot of code into separate files

This function now holds only the actual code to request the
construction of a key structure.  The supporting functions are all
moved to other modules.