minor fix and some refactoring