General refactoring

Here are all the odds and ends that I've been working, mostly small details -- biggest change is moving tests into their own directory and module, and making them runnable by both py.test and untitest (including the doctests).  I brought over all the recent changes from master in e11c503.