massive refactoring for upgrade to 1.2