Problem #23 (improved)