problem18 and 67 refactored