some refactorings to reduce code complexity