Major code reorganization. Cleaned up tests (100% pass).