Huge refactoring, compiles, haven't tried to run it