Removed legacy code, lots of refactoring for easier viewing