Changed memoization implementation to HashSet