some refactoring + tiny optimization