Solution for Problem #40