some refactoring and improvements