some refactoring and code cleaning