Lots of cleanup and tests