Refactor tests and api files

This splits out much of the code that was previously in large files into multiple files.  This should reduce merge conflicts and simplify the code.

This code does introduce a number of pep8 issues.  To be cleaned up in a subsequent patchset.