Memoization to improve performance