Massive refactoring, api and internal changes.

The project compiles but the functionality is broken.