Improved solution for problem #022