WIP. Big cleanup - basic tests are now passing