Refactoring and porting to 3.X