Major code refactoring

Basically all rewritten