some refactoring and one fix