Major refactor. Tests in progress. Automated tests fail right now.