Massive refactoring and 1.6.2 support