some refactorings and fixes