some refactoring and simplifying