Major refactoring, not yet complete