better solution for problem 1-6