massive refactoring
basic internals all implemented