WIP.  Partially working.  Massive code cleanup.