adding caching

Memoization helps performance