few improvements, incl refactoring