Solution for problem #6