some refactoring of problem 1