Major code refactoring in preparation for entities. Regressions still exist.