Solution for Problem #12 (improved++)