Major refactorization. Breaks a lot of stuff.