huge refactoring and code cleaning