Add function memoization and lazy evaluation