Massive refactoring.
Build system fixes.