refactoring and some cleaning