solve problem 2, refactor problem 1