Solution for problem #2