Major refactoring.  Will likely break things...