Massive refactoring, going to be extremely buggy I suspect.