Some more major refactoring. Still a bit broken.