some refactoring + exceptions handling