big rafactoring and clean-up