A better way to solve this question.