added memoizing functionality