Work in progress, major refactoring.