some refactoring and improvements.