Mayor refactoring, code is still being ported.